Executing CRM Workflow programmatically from an external application. CRM 4.0 using ExecuteWorkflowRequest


There is a method named ExecuteWorkflowRequest using which we can execute our workflow programmatically. We had a requirement to find all the opportunities which haven’t been modified for past 30 days and to decrease their probability attribute value by 10.

Now the thing over here was that there wasn’t any specific event against which we could have fired the above workflow. So we thought of writing an application which than we could scheduled, which will periodically run the above workflow

This is how we implemeneted it within a windows application

private void Form1_Load(object sender, EventArgs e){

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.OrganizationName = “organizationName”;

//0 – AD

//1 – Passport

//2 – Form Authentication

token.AuthenticationType = 0;

CrmService crmService = new CrmService();

crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;

crmService.CrmAuthenticationTokenValue = token;

crmService.Url = http://servername:port/mscrmservices/2007/crmservice.asmx”;

try{

// Create an ExecuteWorkflow request.

ExecuteWorkflowRequest request = new ExecuteWorkflowRequest();

//Assign the ID of the workflow you want to execute to the request.

// use this query to get the id select parentworkflowid,name,* from dbo.Workflow

// id is the parentworkflowid

request.WorkflowId = new Guid(“21B9528D-D13D-4B93-9F91-FA7468D3C82C”);

// We want to run it against all the opportunity which are in open state

ArrayList OpportunityGuids = GetOpportunityGuids(crmService);

foreach (String oppGuid in OpportunityGuids){

//Assign the ID of the entity to execute the workflow on to the request.

request.EntityId = new Guid(oppGuid);

ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)crmService.Execute(request);}

// Execute the workflow. }

catch (SoapException ex){

// write in log}

catch (Exception ex){

// write in log} }

private ArrayList GetOpportunityGuids(CrmService crmService){

// using QueryByAttribute to retrieve all the opportunity having statuscode as 1 i.e. Open

QueryByAttribute myOppQuery = new QueryByAttribute();

myOppQuery.Attributes = new String[] { “statuscode” };

myOppQuery.Values = new String[] {“1”};

ColumnSet myCols = new ColumnSet();

myOppQuery.ColumnSet = myCols;

myOppQuery.EntityName = EntityName.opportunity.ToString();

WindowsFormsApplication2.CrmSdk.BusinessEntityCollection myOppCollection= crmService.RetrieveMultiple(myOppQuery);

ArrayList opportunityGuids = new ArrayList();

foreach (WindowsFormsApplication2.CrmSdk.BusinessEntity opp in myOppCollection.BusinessEntities ){

opportunity myOpp = (opportunity)opp;

opportunityGuids.Add(myOpp.opportunityid.Value.ToString()); }

return opportunityGuids;

}

Bye ..

Error: The request failed with HTTP status 401 or Error 401.2: Unauthorized while using CrmService in ASP.NET or windows application CRM 4.0


To use CrmService or Metadata Service (CRM 4.0) within an ASP.NET page or a windows application we need to make use of CRM Authentication token.If we are using Active Directory Authentication this is the code for that

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.OrganizationName = “organizationName”;

//0 – AD

//1 – Passport

//2 – Form Authentication

token.AuthenticationType = 0;

CrmService service = new CrmService();

service.Credentials =System.Net.CredentialCache.DefaultCredentials;

service.CrmAuthenticationTokenValue = token;

service.Url = http://servername:port/mscrmservices/2007/crmservice.asmx”;

try{

WhoAmIRequest myReq = new CrmSdk.WhoAmIRequest();

WhoAmIResponse myResp = (CrmSdk.WhoAmIResponse)service.Execute(myReq);

}

catch (SoapException ex){

Response.Write(ex.Detail.InnerText);

}

catch (Exception ex){

Response.Write(ex.Message);}

 

If we get the above unauthorized access error it could be because either there is problem in our CrmAuthenticationToken, may be we could have assigned wrong organization name or authentication type. If we are using CrmService in an ASP.NET page than we need to use impersonation.

 

<authentication mode=Windows/>

<identity impersonate=true/>

 

If we are using 2006 end point of CrmService we don’t have to use CrmAuthenticationToken.

CallerOrigin property of Plugin Context


We had one of our workflow running on create of lead which was updating one of the attribute in the lead. When the workflow was updating the record one of our plugin registered on the post update event of the lead was getting triggered. This was resulting in an error , so we had to update the post update plugin to include the callerOrigin to handle the things properly

if (context.CallerOrigin.ToString() != “Microsoft.Crm.Sdk.AsyncServiceOrigin”)

////////// our logic

}

CallerOrigin -Using it we can determine the origin of the call. Possible Values are

Static Property Description
Application Gets the caller orgin for the application.
AsyncService Gets the caller orgin for the async service.
WebServiceApi Gets the caller orgin for the Web services.

Bye…

Used SharedVariables in Microsoft Dynamics CRM 4.0


Today we wrote a plugin which was making use of new shared variables concept for plugin in Microsoft Dynamics CRM 4.0

We had one of our plugin registered in post create event of opportunity which was generating a unique number/id for that opportunity record (i.e auto-numbering).
Than we had to write one more plugin again for the same post create event of opportunity but in that plugin we wanted to access the value of that auto generated unique id.
So first what we did was changing the rank of the above two plugin i.e. execution order, if we are registering through plugin-registration tool.
For first plugin which used to generate the unique id we set the rank as 1
and for the second plugin we set it as 2.
But still we were not able to access the value of the unique id attribute in the second plugin.
Than we finally thought of making use of shared variables concept.

The SharedVariables property allows plug-ins to share data with each other. So the plugin which is getting fired first could put some data in the SharedVariables propertybag which than could be accessed by a plugin which is getting fired later.The order of execution of plugin depends on the rank set for it. However we should be careful of not creating too much dependencies among plug-ins.

So, in our first plugin we put the value in the shared variable
context.Properties.Add( new PropertyBagEntry(“MySharedVariable”, myAutoNumber));
and accessed it in the second plugin as following
String mySharedVariable = (string)context.[“MySharedVariable”];

Bye..

Checking for null or blank value in case of post update plugin using ForceSubmit


We were supposed to write a plugin against say entity A which would be updating a related entity B  only if the value of one of the attribute(string)  in entity A is blank.

The plugin was a post update one , so even though we had registered pre/post image with that particular attribute and as it was null, the property/value wasn’t getting passed.

So there was no way to access it in the entity images.

Only way to access the value was to make use of query expression class to get the value saved in the table for that entity. But we decided to use another approach.

Normally if we set a ForceSubmit on a particular field it is passed as one of the property of inputparameters of the context.

So what we did was set ForceSubmit on the field and then converted the inputparameters properties as Dynamic Entity and were able to get the value of that attribute even in case when it was null/blank.

DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[“Target”];

if (entity.Properties.Contains(“new_attSchemaName”))
{
String   myAttrValue = (string)entity.Properties[“new_attSchemaName”];
if (myAttrValue == “”)
{

//

Bye..

Update in pre update plugin in CRM


In previous version i.e. 3.0  on updating certain field in pre/post update used to lead to circular reference.

The only way to update in case of crm 3.0 was to modify the entityxml recieived as ref parameter for preUpdate event handler.

In CRM 4.0 to do the same we can add/update the properties passed as inputparameters to the context for pre-update.

public void Execute(IPluginExecutionContext context)
{
DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties[“Target”];
if (entity.Name == EntityName.lead.ToString())
{

// firstname and lastname are already properties of the entity which are not null and

// therefore are passed as inputparamters

String firstName = “Nishant”;
entity.Properties[“firstname”] = firstName;

String lastName = “Rana”;
entity.Properties[“lastname”] = lastName;

//or setting a value of a field whose value is null i.e. not passed as inputparameter
StringProperty subject = new StringProperty(“subject”, “Test Subject”);
entity.Properties.Add(subject);

}
}

The above plugin is registered for lead’s pre-update event.

Using service.update would lead to “server is unable to process the request error” as it automatically checks for the circular reference, if the crmService has been created using context.CreateCrmService() method.

For CRM 3.0 refer this wonderful article

http://www.stunnware.com/crm2/topic.aspx?id=Callout2

Bye…