Showing posts with label MSCRM Plug-in. Show all posts
Showing posts with label MSCRM Plug-in. Show all posts

Monday, July 8, 2013

MS CRM 2011 - Manage the parent customer field of a contact


The following code set the paterent customer in contact entity. I was unable to find any documentation of this, I hope this will be helpful for you.
// Comment

//for contact entity
EntityReference myRef = null;
myRef= new EntityReference("contact", (Guid)myContact.ContactID); 
if (myRef != null) contactEntity["parentcustomerid"] = myRef; 
// Commit entity to CRMGuid 
Guid result = service.Create(contactEntity);

//for account entity
EntityReference myRef = null;
myRef= new EntityReference("account", (Guid)myContact.AccountID); 
if (myRef != null) contactEntity["parentcustomerid"] = myRef; 
// Commit entity to CRMGuid 
Guid result = service.Create(contactEntity);




Thanks 
Nicola Grillo

Sunday, August 5, 2012

MSCRM 2011 - Plugin View

In this post I will show steps by steps how to implement plugin on Retrieve Multiple Message using the plug-in registration tool for Microsoft Dynamics CRM 2011

1. Step Create a new plugin

2. Add the following  code snippets.  Fieldname is the field that you want to check (example name of account).

// Comment
protected void ExecuteViewController(LocalPluginContext localContext)
        {
            if (localContext == null)
            {
                throw new ArgumentNullException("localContext");
            }
            IPluginExecutionContext context = (IPluginExecutionContext)localContext.PluginExecutionContext;
            IOrganizationService service = localContext.OrganizationService;
            QueryExpression qe = (QueryExpression)context.InputParameters["Query"];
            if (qe.Criteria.Conditions != null)
            {
                ConditionExpression queryCondition = new ConditionExpression("fieldname", ConditionOperator.Equal, "value");
                context.InputParameters["Query"] = qe;
            }
        }


3. Add plugin step (example: Account on entity)


4. If you want to add other steps you must repeat third step and you must register the associate event as below.

// Comment
public FilteredViewController()
            : base(typeof(FilteredViewController))
        {
            base.RegisteredEvents.Add(new Tuple>(20, "RetrieveMultiple", "appointment", new Action(ExecuteFilteredViewController)));
            base.RegisteredEvents.Add(new Tuple>(20, "RetrieveMultiple", "account", new Action(ExecuteFilteredViewController)));
            base.RegisteredEvents.Add(new Tuple>(20, "RetrieveMultiple", "lead", new Action(ExecuteFilteredViewController)));
            // Note : you can register for more events here if this plugin is not specific to an individual entity and message combination.
            // You may also need to update your RegisterFile.crmregister plug-in registration file to reflect any change.
        }


Notes:
Previous code snippets (ExecuteViewController) can be used for each entities that contains the target field (fieldname).


Thanks,
Nicola Grillo

Saturday, May 19, 2012

MSCRM 2011 - Retrieve Attachments by ID

// Comment
QueryExpression query = new QueryExpression

                    {
                        EntityName = ActivityMimeAttachment.EntityLogicalName,
                        ColumnSet = new ColumnSet("activitymimeattachmentid"),
                        Criteria = new FilterExpression
                        {
                            Conditions =
                        {
                            new ConditionExpression
                            {
                                AttributeName = "objectid",
                                Operator = ConditionOperator.Equal,
                                Values = {"activity/customentity ID"}
                            },
                            new ConditionExpression
                            {
                                AttributeName = "objecttypecode",
                                Operator = ConditionOperator.Equal,
                                Values = {Email.EntityLogicalName}
                            }
                        }
                        }
                    };
EntityCollection results = service.RetrieveMultiple(query);


Nicola Grillo

Friday, January 20, 2012

CRM 2011 - ActivityParty System.Runtime.Serialization.SerializationException

When you start a plug-in assembly in the Microsoft Dynamics CRM 2011 client for Microsoft Office Outlook and you receive the following exception message:

Exception

The Web Service plug-in failed in OrganizationId: b4a5466e-8038-080027d06cae; SdkMessageProcessingStepId: 5263132e-3584-410d-ad1f-70702a62; EntityName: appointment; Stage: 15; MessageName: Create; AssemblyName: Microsoft.Crm.OfflineQueuePrimaryKeyPlugin, Microsoft.Crm.ObjectModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31b56ad364e35; ClassName: Microsoft.Crm.OfflineQueuePrimaryKeyPlugin; Exception: Unhandled Exception: System.Runtime.Serialization.SerializationException: Type 'Xrm.ActivityParty[]' with data contract name 'ArrayOfActivityParty:http://schemas.datacontract.org/2004/07/Xrm' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteKeyValuePairOfstringanyTypeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteAttributeCollectionToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteEntityToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteKeyValuePairOfstringanyTypeToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteParameterCollectionToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteOrganizationRequestToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
   at Microsoft.Crm.BusinessEntities.SoapContext.SerializeSdkRequest(OrganizationRequest requestToSerialize, Entity target, ExecutionContext context)
   at Microsoft.Crm.BusinessEntities.SoapContext.SerializeRequestWithEntity(DynamicEntity dynamicEntity, IOrganizationContext context)
   at Microsoft.Crm.BusinessEntities.SoapContext.GenerateModifiedSoapPacket(DynamicEntity entity, String requestName, String targetName, IOrganizationContext context)
   at Microsoft.Crm.OfflineQueuePrimaryKeyPlugin.Execute(IServiceProvider serviceProvider)
   at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context)
   at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)



This exception occurs when you use XRM.ActivityParty in your Offline Plugin. In this case you have to change your Plug-in code in order to solve this issue.


Example :


If you use the following code:


Example

ActivityParty activitypartyentity=new ActivityParty();
....

Or

ActivityParty[] arrayofactivityparty=....
You have to change your code as the following:

Example

Entity activitypartyentity=new Entity("activityparty");
....
..
or


Entity[] arrayofactivityparty=....



Nicola Grillo

Wednesday, January 11, 2012

CRM 2011 - Plugin Error System.InvalidCastException: Unable to cast object of type 'xxxx' to type 'Microsoft.Crm.Sdk.IPlugin'.

This error occurs when in your plug-in have an reference code conflict to the Microsoft Dynamics CRM SDK 4.0 . In order to solve this issue you need to remove all  Microsoft Dynamics CRM 4.0 conflicts!


Nicola Grillo

Monday, January 9, 2012

CRM 2011 - Plugin registration tool Error only Isolate plugin


I got this error after latest update with the Roll 4 to the plugin registration tool when i tried to registry a new assembly . I solve it with a remove of the Connections.config.xml in the plug-in registration tool folder.


Nicola Grillo

Thursday, December 29, 2011

CRM 2011 - Appointment plug-in in Pre-validation/Pre-creation stage with Create and Book message

If you want to registry a new plugin in CreateRequest message you have to consider that it can't be started.
Considering the following example :
You want subscribe, or register, an Appointment plug-in to the "pre-validation CreateRequest", in order to modify some appointment field.We would expect that it will run when the event occurs.

Now create a new appointment with start and end date 5 day after today.........surprise............!!!!!!! it doesn't run.
In this case the BookRequest message occurs and you have to subscrive, or register also your plug-in to BookRequest message, in order to triggering this event.

Nota:
I'm not sure but the BookRequest message should be substitute the information in the previous stage (CreateRequest in pre-validation) .

This shold be a big problem because if you want to use the Appointment plugin in ofline mode it doesn't work. Considering that the Book message work only in server mode!

In the following list you can find all appointment message :


MessageDescription
Adds recurrence information to an existing appointment to make it a recurring appointment master.
Assigns an appointment to another user or team.
Creates a link between a record and one or more records that participate in a relationship. You can also use theIOrganizationService.Associate method. For more information, seeActions on Entity Records. For a list of default relationships for this entity, see Appointment Entity Metadata.
Schedules or books an appointment.
Creates an appointment. You can also call theIOrganizationService.Create method.
Creates an exception for the recurring appointment instance,Appointment.InstanceTypeCode. For more information, seeCreate a Recurring Appointment Exception.
Deletes an appointment. You can also call theIOrganizationService.Delete method.
Removes a link between a record and one or more records that participate in a relationship. You can also use theIOrganizationService.Disassociate method. For more information, see Actions on Entity Records. For a list of default relationships for this entity, see Appointment Entity Metadata.
Shares an appointment with another security principal (user or team).
Initializes a new record from an existing record provided that an attribute map is defined for the two entities. For more information, see Customize Entity and Attribute Mappings.
Changes the access rights for an appointment previously shared with another security principal (user or team).
Reschedules an appointment.
Detects and retrieves duplicate records for the specified record, provided that duplicate detection is enabled and there are published duplicate detection rules for the entity. For more information, seeDetect Duplicate Data in Microsoft Dynamics CRM.
Retrieves an appointmentYou can also call theIOrganizationService.Retrieve method.
Retrieves a collection of appointments. You can also call theRetrieveMultiple method.
Retrieves the access that the specified security principal (user or team) has to an appointment.
Retrieves the security principals (users or teams) that have access to an appointment, together with their access rights to the appointment.
Removes all access to an appointment for the specified security principal (user or team).
Sets the state of an appointment. The possible values for this attribute are defined in the Appointment.State attribute. If you are using early bound types, you can use the AppointmentStateenumeration.
Updates an appointment. You can also call the Update method.
Validates that all the constraints of an appointment are met.



Nicola Grillo


Reference:
Microsoft Dynamics CRM 2011 SDK

Wednesday, October 26, 2011

CRM 2011 - Use IOrganizationService in Plugin MSCRM 2011.

Following code allow connects to the Microsoft Dynamics CRM 2011 platform using the IOrganizationService Web Service.

// Comment
//use your prefered userId to get IOrganicazionService by context
private IOrganizationService getService(IServiceProvider serviceProvider, Guid userId)
        {
            IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            return serviceFactory.CreateOrganizationService(userId);
        }


Thanks,
Nicola Grillo