Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, February 23, 2012

Get the Context instance of ms crm workflow and plugin

To get the context of ms crm is the first phase every MS CRM developer come across when going to develop a custom workflow or plugin. The context instance of workflow or plugin gives us the details of the entity on which entity record this workflow is running which helps us to fetch more details to lead the custom logic.

//Workflow
[CrmWorkflowActivity("My Test Workflow")]
public partial class MyTestWorkflow : SequenceActivity
{
        #region Designer generated code
        ---
        ---
        #endregion
      protected override ActivityExecutionStatus Execute(ActivityExecutionContext context)
      {
         IContextService contextService = (IContextService)context.GetService(typeof(IContextService));                                  
         IWorkflowContext workflowContext = contextService.Context;
         Guid entityID = (Guid)context.OutputParameters.Properties["id"];
      }
}

//Plugin
public class Plugin:IPlugin
{
    public void Execute(IPluginExecutionContext context)
    {
        DynamicEntity entityInstance;
        if (context.InputParameters.Properties.Contains("Target") && 
               context.InputParameters.Properties["Target"] is DynamicEntity)
        {
             entityInstance= (DynamicEntity)context.InputParameters.Properties["Target"];             
        }                        
    }     
}

MS CRM Retrieve and RetrieveMultiple methods do not work on Custom or Dynamic Entities

RetrieveMultiple do not work to retrieve data from custom entities and it ends with the runtime exception if we try to get the records using this method.
Exception: Error in XML document. The specified type was not recognized.

This is because RetrieveMultiple works on strongly typed entities only. When we make use of SDK's we return custom entities as dynamic entities which are not strongly typed. In order to retrieve the Dynamic Entity, we should use Execute method as shown in the below code.

RetrieveMultipleRequest req = new RetrieveMultipleRequest();
req.Query = queryExpression;
req.ReturnDynamicEntities = true;
RetrieveMultipleResponse resp = (RetrieveMultipleResponse)_service.Execute(req);
if (resp != null && resp.BusinessEntityCollection.BusinessEntities.Count > 0)
{
  Guid columnID=Guid.Empty;
  dynEntity = (DynamicEntity)resp.BusinessEntityCollection.BusinessEntities[0];
  if ( dynEntity.Properties.Contains("new_columnid"))
     columnID = ((Lookup)dynEntity.Properties[" new_columnid"]).Value;
}

If we use crm webservices instead of sdk, we can make use of RetrieveMultiple without any issues.

Dynamically update or assign the owner of an entity in MS CRM 4.0

In general we use the Update method to update the fields in MSCRM. But, we cannot update the owner of an entity using update method. To update the owner, we have to make use of AssignRequest.

AssignRequest req = new AssignRequest();

SecurityPrincipal assignee = new SecurityPrincipal();
assignee.Type = SecurityPrincipalType.User; //User or Queue
assignee.PrincipalId = ownerID; //crm userid to whom we assign 

// Request changes depending on the entity. For account, it is TargetOwnedAccount
TargetOwnedLead target = new TargetOwnedLead(); 
target.EntityId = leadID; //Id of the lead record whose owner has to be updated

req.Assignee = assignee;
req.Target = target;
_service.Execute(req); //_service is the crm service