Understanding a Hello World program in Windows Workflow Foundation

Hi,

1. Create a new Sequential Workflow Console Application project.

2. Open Workflow1.cs in design mode.

3. Drag a Code activity to the workflow at (Drop activities to create a sequential workflow)

4. Select Code activity and in the properties window for ExecuteCode property write MyMessage and the press enter twice, which will create a method with the same name.

private void MyMessage(object sender, EventArgs e)
{
Console.Write(“Hello World”);
Console.ReadLine();
}

5. Press F5 to run the project.

This completes our first program in windows workflow foundation.

What we have done over here is that

We have created a console application which is hosting a workflow. Workflow requires an application as a host. It could be a console application, windows forms application,

asp.net application and even a Windows service.

We can found the code used for hosting the workflow in Program.cs.

Let’s try to understand the code

// First we are creating an instance of WorkflowRuntime

// WorkflowRuntime – is responsible for starting our workflow, firing events for different situations

// while the task is executing.

using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())

{

// Our application and the workflow, both are running on different threads.

// We need our application to wait long enough before our workflow either gets completed or terminated(due to error).

// For this AutoResetEvent is used.

// Here an instance of AutoResetEvent is created i.e. waitHandle

// calling waitHandle.WaitOne() will make the main thread to wait until it is signalled using

// waitHandle.Set(), which will be done when workflow either gets complete or gets terminated.

AutoResetEvent waitHandle = new AutoResetEvent(false);

 

// Here anonymous method is used to declare an event handler for WorkflowCompleted event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set()

workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e)

{waitHandle.Set();};

 

// Here anonymous method is used to declare an event handler for WorkflowTerminated event

// Once workflow gets completed, the main thread is signalled to proceed further through

// waitHandle.Set(), which releases the console application from its wait

workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)

{

Console.WriteLine(e.Exception.Message);

waitHandle.Set();

};

 

// Workflow instance is composed of one or more activities. WorkflowRuntime executes these workflow instances.

// workflowRuntime.CreateWorkflow->Creates a workflow instance and if the workflow runtime hasn’t been started

// the CreateWorkflow method calls StartRuntime method of WorkflowRuntime

WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(HelloWorldWorkflow.Workflow1));

 

//Starts the execution of the worflow instance

instance.Start();

 

// The main thread waits till it is signalled to proceed further i.e. waitHandle.Set()

waitHandle.WaitOne();

}

Bye…

Closing an opportunity programmatically CRM

We can make use of following for closing the opportunity programmatically in CRM.

WinOpportunityRequest for closing an opportunity as won

//We create an instance of WinOpportunityRequest class as salesstage is Won.
WinOpportunityRequest woReq=new WinOpportunityRequest();
//WinOpportunityRequest takes 2 values: Opportunityclose and Status.
opportunityclose oc=new opportunityclose();
//Since Opportunityid is of type Lookup, we create an instance of the Lookup class. Lookup class has 2 attributes: Lookup.Value and Lookup.Type
Lookup lookup=new Lookup();
//We pass the GUID value of the opportunity to an instance of the Lookup class.
lookup.Value=entityContext.InstanceId;                                                                                          //We specify the type of the entity being passed.
lookup.type=EntityName.opportunity.ToString();
oc.opportunityid=lookup;
oc.actualend=actualclosedate(CrmDateTime variable);
oc.actualrevenue=new_estimatedrevenue(CrmMoney variable);
woReq.OpportunityClose=oc;

//The Status parameter corresponds to Status Reason in the Microsoft CRM application. If you pass -1 for this parameter, the platform sets the status to the appropriate value for the Microsoft CRM application.
woReq.Status=-1;
//The WinOpportunityRequest is sent to the platform using the Execute method. The platform will run the request and send back an instance of the Response class message.
WinOpportunityResponse woRes=(WinOpportunityResponse)service.Execute(woReq);


LoseOpportunityRequest for closing opportunity on lost and dropped
LoseOpportunityRequest loReq=new LoseOpportunityRequest();
//LoseOpportunityRequest takes 2 values: Opportunityclose and Status.
opportunityclose oc=new opportunityclose();
//Since Opportunityid is of type Lookup, we create an instance of the Lookup class. Lookup class has 2 attributes: Lookup.Value and Lookup.Type
Lookup lookup=new Lookup();
//We pass the GUID value of the opportunity to an instance of the Lookup class.
lookup.Value=entityContext.InstanceId;
//We specify the type of the entity being passed.
lookup.type=EntityName.opportunity.ToString();
oc.opportunityid=lookup;
oc.actualend=actualclosedate;
oc.actualrevenue=new_estimatedrevenue;
loReq.OpportunityClose=oc;
//The Status parameter corresponds to Status Reason in the Microsoft CRM application. If you pass -1 for this parameter, the platform sets the status to the appropriate value for the Microsoft CRM application.
loReq.Status=-1;
//The LoseOpportunityRequest is sent to the platform using the Execute method. The platform will run the request and send back an instance of the Response class message.
LoseOpportunityResponse loRes=(LoseOpportunityResponse)service.Execute(loReq);

Bye

Creating and Sending email in CRM using SendEmaiRequest

We can use the following code for creating and sending an email in CRM

/// <summary>
/// Creating and Sending mail in CRM
/// </summary>
/// <param name=”crmService”>an instance of crmService  (CrmService for CRM 3.0)</param>
/// <param name=”members”>ArrayList containing guids of the members</param>
/// <param name=”msg”>The message body</param>
/// <param name=”ownerID”>Owner of the record</param>
/// <param name=”userID”>User under whose context the callout or plugin is running </param>

private void SendMailToMembers(ICrmService crmService, ArrayList members, String msg, String ownerID, String userID)
{

// create an email
email emailCreate = new email();
emailCreate.subject = “MySubject”;
emailCreate.description = msg;

//specify the owner for the mail
emailCreate.ownerid = new Owner();
emailCreate.ownerid.type = EntityName.systemuser.ToString();
emailCreate.ownerid.Value = new Guid(ownerID);

//create an activityparty array holding all the guids specified in the members array list
activityparty[] ap = new activityparty[members.Count];
// creating as many activity party as the no of users or members in a team
int i = 0;
foreach (String memberID in members)
{
ap[i] = new activityparty();
ap[i].partyid = new Lookup();
ap[i].partyid.type = EntityName.systemuser.ToString();
ap[i].partyid.Value = new Guid(memberID);
i++;
}

// specify to part of the email
emailCreate.to = ap;

// specify the from part of the email
activityparty from = new activityparty();
from.partyid = new Lookup();
from.partyid.type = EntityName.systemuser.ToString();
from.partyid.Value = new Guid(userID);
emailCreate.from = new activityparty[] { from };

// finally create the email and get the guid of the email
Guid emailId = crmService.Create(emailCreate);

// FOR CRM 3.0
// Specify the system user who is sending the message.
//crmService.CallerIdValue = new CallerId();
//crmService.CallerIdValue.CallerGuid = new Guid(userID);
//

// Create an SendEmailRequest object
SendEmailRequest req = new SendEmailRequest();
req.EmailId = emailId;
req.TrackingToken = “”;
req.IssueSend = true;

// Finally Send the email message.
SendEmailResponse res = (SendEmailResponse)crmService.Execute(req);
}

Bye

PreEntity and PostEntity Images in CRM 4.0

Hi ,

When writing callouts for CRM 3.0, we were provided with PreEntityImageXml and PostEntityImageXml as a parameter to methds being overrided by us.

We could specify the fields for the same in the callout.config as prevalue and postvalue tag.

But the things have changed a bit in CRM 4.0.

Here we have to implement the Execute method found in the interface IPlugin.

public void Execute(IPluginExecutionContext context)

Here first we will register our assembly using Plugin Registration Tool

Than we will register a new step and specify the message against which we want to run our plugin.

After this comes the step where we will Register  new image.

In Register New Image dialog box

We can specify Pre Image and Post Image depending upon pre or post event. (Only for post event we can have both pre and post image)

Parameters -Here we can either specify certain fields or can select all attributes.

Entity Alias – Give a name to our image which we will refer in our code. (eg. say we gave LeadImage)

Now to access these values within our plugin we need  to do the following:-

Cast it into a DynamicEntity
DynamicEntity preLead = (DynamicEntity)context.PreEntityImages[“LeadImage”];
DynamicEntity postLead = (DynamicEntity)context.PostEntityImages[“LeadImage”];

For getting the id of the entity ( unlike crm 3.0 we don;t have entity context over here )

Key keyLeadId = (Key)postLead.Properties[“leadid”];
string leadId = keyLeadId.Value.ToString();

For lookup field use this line of code

Lookup lkpLobPre = (Lookup)preLead.Properties[“new_linesofbusinessid”];
strPreLobGuid = lkpLobPre.Value.ToString();
Lookup lkpLobPost = (Lookup)postLead.Properties[“new_linesofbusinessid”];
strPostLobGuid = lkpLobPost .Value.ToString();

For owner field

Owner ownerLead = (Owner)postLead.Properties[“ownerid”];
strPostOwnerGuid = ownerLead.Value.ToString();

For money field

CrmMoney estimatedvalue = (CrmMoney)postLead[“new_expectedrevenue”];
strExpectedRevenue = estimatedvalue.Value.ToString();

For string field

String geoName = (String)postLead[“new_geographyidname”];

For picklist field

Picklist leadqualitycode = (Picklist)postLead[“leadqualitycode”];
strRating = leadqualitycode.name;

Bye

Unable to cast object of type ‘Microsoft.Crm.Sdk.Moniker’ to type ‘Microsoft.Crm.Sdk.DynamicEntity’

I was writing my first callout ( plugin) for CRM 4.0 when i recieved this error.

I realized that this was because of this line of code in my plugin

public void Execute(IPluginExecutionContext context)
{

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

……

For Create and Update message there was no problem.

Problem started coming when i registered the step for Assign message.

So modified the code as following

DynamicEntity entity = null;
if (context.InputParameters.Properties.Contains(“Target”) &&
context.InputParameters.Properties[“Target”] is DynamicEntity)
{
// Obtain the target business entity from the input parmameters.
entity = (DynamicEntity)context.InputParameters.Properties[“Target”];
}

For Assign Message used the following line of code

if (context.MessageName == “Assign”)
{
Moniker myMoniker = null;
if (context.InputParameters.Properties.Contains(“Target”) &&
context.InputParameters.Properties[“Target”] is Moniker)
{
// Obtain the target business entity from the input parmameters.
myMoniker = (Moniker)context.InputParameters.Properties[“Target”];
}

myMoniker.Name –> Gave the name of the entity

myMoniker.Id –> Gave the id of the entity

Bye

Retrieving Members of Team – CRM

Hi,

We can make use of the following function for retrieving the members of a particular team

It takes as parameter instance of a ICrmService(CRM 4.0) or CrmService (CRM 3.0) and the guid of the team and returns an arraylist of of all the members found in the team

public ArrayList GetMembersOfTeam(ICrmService service, String TeamID)
{
// We want the guid of the system users in the team
ColumnSet cols = new ColumnSet(new String[] { “systemuserid” });
// We need to make use of RetrieveMembersTeamRequest class for that
RetrieveMembersTeamRequest rmtRequest = new RetrieveMembersTeamRequest();
// Converting the string TeamID to guid
Guid headGuid = new Guid(TeamID);

rmtRequest.EntityId = headGuid;
rmtRequest.MemberColumnSet = cols;

RetrieveMembersTeamResponse retrieved = (RetrieveMembersTeamResponse)service.Execute(rmtRequest);

// using generic businessEntity list
List<BusinessEntity> sysResult = retrieved.BusinessEntityCollection.BusinessEntities;

// for CRM 3.0
// BusinessEntity[] sysResult = retrieved.BusinessEntityCollection.BusinessEntities;

ArrayList userGuid = new ArrayList();

foreach (BusinessEntity be in sysResult)
{
systemuser user = (systemuser)be;
userGuid.Add(user.systemuserid.Value.ToString());
}
return userGuid;
}

Bye