Mohammed Atef’s Technical blog

CRM Dynamics 4.0 Work Flow

One important consideration is that Workflows in Microsoft Dynamics CRM are executed asynchronously, and use a Windows service to act as a Host application for the Workflow engine to work. This Windows service is called the Microsoft CRM Asynchronous Processing Service and must be running on the server or else the Workflows won’t execute. Notice that if this service is not running, you won’t see any of the Workflows you created. However, they will run as soon as you start this service because they are queued.

By default, any valid CRM user can create Workflows; however, the permissions to create Workflows can be configured by roles, to prevent users from creating new Workflows that might corrupt the system data.

Workflow Events

Workflow Manager include additional triggering events and run settings.
The events that trigger a Workflow are:
* Record is created  * Record status changes
* Record is assigned * Record attributes change * Record is deleted

Workflow Scope

Workflows created through this interface can be applied to the following areas:
* Users * Business units * Parent: Child business units * Organization

Exporting and Importing Workflows

You can transfer Workflows from one organization to another by exporting them as compressed zip files and then importing them into the new system.

Export

To export a Workflow, navigate to Settings, Customization, Export Customizations. Select Workflows from the View drop-down menu
You can export all Workflows by clicking on More Actions and selecting Export All Customizations or export only the selected Workflows.
The Workflow is downloaded as a compressed zip file. Inside the compressed zip file resides an XML file named customizations.xml.

Import

To import a Workflow, follow these steps:
1- Go to Settings, Customization, Import Customizations
2- Enter the local path of the customization file or click the Browse button to locate the customized Workflow file on your hard disk. Click Upload to continue.

Creating Workflows in Windows Workflow Foundation (WWF) with Visual Studio

we can use work flow with Microsoft CRM Dynamics without writing any number of line code

No-Code Workflow

The CRM 4.0 SDK comes with a new Visual Studio project template to build No-Code Workflows. These No-Code Workflows are Extensible Object Markup Language (XOML) files—they contain Workflow markup in XML format.
The advantages of No-Code Workflows include the following:
* Can be deployed without compiling * Easier to develop
* Can be deployed on live CRM servers or on-premise servers
* Can share WWF activities by adding parallel tasks or loop/while conditions
Before being able to create a No-Code Workflow, you need to install the CRM Workflow project templates. To install these templates, follow these steps:
1- Locate the solution file with the name WorkflowConfigurationTool.sln in the SDK folder\Tools and open the solution file with Visual Studio 2005.
2- Add the missing references Microsoft.Crm.Sdk.dll and Microsoft.Crm.SdkTypeProxy.dll to the CrmWorkflowConfigurationAndDeployment project.
3- Update the web references for the two Web Services and change the compilation mode to release then build the entire solution.
4- Close Visual Studio, go to the SDK\Tools\DeployWorkflowTool\Release folder and execute the setup.exe, click next to confirm installation, click next to continue.
Now you can start Visual Studio 2005 and start creating CRM Workflow assemblies. From Visual Studio, go to File, New, and Project. Expand Visual C# node, click CRM node, and select CRM 4.0 Workflow.
When you create a new project using this template, you will see a new group of activities specially designed to be used with CRM
Note, If you don’t see these activities in the toolbox, right-click the General Tab and click the Choose Items menu option. Then browse and add the Microsoft.Crm.Sdk.dll, which is located in the SDK\bin folder.
To configure and deploy these Workflows, you must install a Visual Studio add-in that comes with the CRM SDK. The add-in installer filename is DeployAddin.msi. Install it by double-clicking the file and following the installation procedure.
The following example shows how to build a Workflow that will create a Phone Call activity for every new Contact added to CRM.
To create the sample, you first must perform the following configuration tasks.

Configuration

Go to the Tools menu in Visual Studio and select the Microsoft Dynamics CRM menu option, Configure Workflow
1- The first screen asks you to choose whether you want to configure the deployment for CRM live or an on-premise CRM server
2- Select the On-Premise option and enter the CRM server address, username, password, and Windows domain name
3- Select the organization where you want to deploy the Workflow then enter a name for the Workflow
4- In the General section, locate the XOML Workflow filename on your hard disk by clicking Browse
5- Move to the Primary Entity selection in the list box and click Update List because all the entity types might not show by default.
6- Select the entity you want to associate the Workflow with. For this example, we use the Contact entity ,In SDK Events, check the Create option
7- Move to local parameters and enter NewContactWelcomePhoneCall as the Parameter name and select the Microsoft.Crm.Sdk.DynamicEntity in the Type Name drop-down box. Then click Add You can create any properties you need here.
8- Click Save and Close.

Development

To develop the Workflow, follow these steps
1- Select the Policy activity, which is under the Windows Workflow section in the toolbox, and drag it into the CrmWorkflow1.xoml file or double-click the item and it will be automatically added.
2- Go to the Properties window and select the RuleSetReference property; then click the … button,Click the New button ,Click the Add Rule button and In the Condition section, enter true
3- In the Then section, enter the following code:

   1: this._NewContactWelcomePhoneCall =Microsoft.Crm.Workflow.CrmWorkflow.CreateEntity("phonecall")
   2: this._NewContactWelcomePhoneCall["subject"] = "Call this new contact"
   3: 

4- Click OK to close the Rule Set Editor. And Click OK to close the RuleSetReference dialog box.

5- Select the CreateActivity item that is in the toolbox under the General group, drag this activity to the Workflow under the PolicyActivity you added in step 1, and go to the properties window.

If you don’t see these activities in the toolbox, right-click the General Tab and click the Choose Items menu option. Then browse and add the Microsoft.Crm.Sdk.dll which is located in the SDK\bin folder

6- Select the Entity property and click on the … button.

7- Select the NewContactWelcomePhoneCall property and Click OK to continue and Save WorkFlow at the end The resulting Workflow should look similar to this figure

image

Deployment

1- Go to the Tools menu in Visual Studio and select the Microsoft Dynamics CRM menu option, followed by Configure Workflow.

2- Click the Deploy button and click Yes to continue and To publish the Workflow click Yes

3- Click OK to continue and Click Save and Close

To see the Workflow that was just deployed, open Internet Explorer and go to the CRM web interface. Go to Settings and then to Workflows.

You can also see the deployed Workflows and publish or unpublish them from Visual Studio. Go to the Tools menu, select the Microsoft Dynamics CRM menu option, and then select Manage Workflows.

Custom Workflow Activities

Custom workflow activities are used to build workflow with code behind using WWF. The difference between them and No-Code workflows that we reviewed previously is that custom workflow activities are compiled in dynamic link libraries (DLL) and can be used from the Workflow interface found in either the Web or Outlook client applications as new steps. To create a custom workflow activity, open Visual Studio 2005 and create a new project using the Workflow Activity Library template that is inside the Visual C#, Workflow project.

After creating the project, you will have to add the references for microsoft.crm.sdk.dll and microsoft.crm.sdktypeproxy.dll.

With this project, you can add any workflow activity from the toolbox as well as add code in C# in the code behind if desired. For our example, we use the same sample we used for the No-Code workflow but this time with a custom activity. Go to see the code behind and replace any existing code with the following:

   1: using System;
   2: using System.ComponentModel;
   3: using System.ComponentModel.Design;
   4: using System.Collections;
   5: using System.Drawing;
   6: using System.Workflow.ComponentModel.Compiler;
   7: using System.Workflow.ComponentModel.Serialization;
   8: using System.Workflow.ComponentModel;
   9: using System.Workflow.ComponentModel.Design;
  10: using System.Workflow.Runtime;
  11: using System.Workflow.Activities;
  12: using System.Workflow.Activities.Rules;
  13: using Microsoft.Crm.Workflow;
  14: using Microsoft.Crm.Sdk;
  15: 
  16: namespace CustomActivityLibrary
  17: {
  18:     [CrmWorkflowActivity("Custom Activity", "Custom Activities Library")]
  19:     public partial class CustomActivity : SequenceActivity
  20:     {
  21:         public CustomActivity()
  22:         {
  23:             InitializeComponent();
  24:         }
  25:         public static DependencyProperty myContactProperty =
  26: DependencyProperty.Register("myContact", typeof(Lookup), typeof(CustomActivity));
  27: 
  28:         [CrmInput("My contact")]
  29:         [CrmReferenceTarget("contact")]
  30:         public Lookup myContact
  31:         {
  32:             get
  33:             {
  34:                 return (Lookup)base.GetValue(myContactProperty);
  35:             }
  36:             set
  37:             {
  38:                 base.SetValue(myContactProperty, value);
  39:             }
  40:         }
  41:         protected override ActivityExecutionStatus Execute(
  42:                                        ActivityExecutionContext executionContext)
  43:         {
  44:             // Get the context service.
  45:             IContextService contextService =
  46:                 (IContextService)executionContext.GetService(typeof(IContextService));
  47: 
  48:             IWorkflowContext context = contextService.Context;
  49: 
  50:             // Use the context service to create an instance of CrmService.
  51:             ICrmService crmService = context.CreateCrmService();
  52: 
  53:             // Creates the Phone Call activity for this conatct
  54:             Microsoft.Crm.SdkTypeProxy.phonecall nyPhoneCall =
  55:                                new Microsoft.Crm.SdkTypeProxy.phonecall();
  56:             nyPhoneCall.subject = "Call this new contact";
  57:             nyPhoneCall.regardingobjectid = myContact;
  58:             crmService.Create(nyPhoneCall);
  59:             return ActivityExecutionStatus.Closed;
  60:         }
  61:     }
  62: }

Build the solution in debug mode to create the assembly.

To deploy the custom workflow activities, register the compiled assembly as a Plug-In using the same plugindeveloper tool we used in, with minor changes to the register.xml file. Here is an example of how the register.xml file should look for our sample:

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <Register
   3:     LogFile = "Plug-in Registration Log.txt"
   4:     Server  = "http://crm4"
   5:     Org     = "Webfortis"
   6:     Domain  = "Webfortis"
   7:     UserName= "administrator" >
   8: 
   9:         <Solution SourceType="0" Assembly="C:\CRM4 book\Chapter 20\CustomActivityLibrary\CustomActivityLibrary\bin\Debug\CustomActivityLibrary.dll">
  10:                 <WorkflowTypes>
  11:                         <WorkflowType TypeName="CustomActivityLibrary.CustomActivity" FriendlyName="Custom Activity"/>
  12:                 </WorkflowTypes>
  13:         </Solution>
  14: </Register>

After deploying the custom workflow activity, you can use it on any Workflow. To do this, go to Settings, Workflow, and click New. Enter a name for the workflow and select Contact in the Entity. Click OK to move to the next step. Then click Add Step and you will see the new group called Custom Activities Library with our Custom Activity inside .

Add a step with the Custom Activity and click the Set Properties button.You can set any custom property you added on the code. Our example sets the My Contact property we used to send the current contact where the workflow will be running Click Save, Close to close the Set Custom Step Input Properties and then click Save to save the workflow. Click Publish to test this solution.

important note,You can install custom workflow activities on On-Premise servers only, because they are not supported by CRM Live servers.

Advertisements

February 5, 2009 - Posted by | CRM Dynamics 4.0, MS Dynamics | , ,

6 Comments »

  1. Create pure drinking water with our Atmospheric Water Generators
    http://tinyurl.com/d28fdp

    Comment by TrereeKaw | February 18, 2009 | Reply

  2. I must be totally retarded…. I’m a newbie for sure.

    But I cant get the no-code version to work in Visual Studio 2008?

    Since in the latest SDK the tool you reference is not even available and hence the sub section of CRM is never under creating a new project.

    Do you have any help on how to repeat the process you do above with windows 2008?

    I’m subscribed to the comments here, or you can email me. Would greatly appreciate any help you have to offer.

    Thanks
    Marc

    Comment by Marc Lindsay | May 5, 2009 | Reply

  3. hello,
    you don’t found WorkflowConfigurationTool.sln or crm reference Microsoft.Crm.Workflow and Microsoft.Crm.Sdk

    Comment by mohammedatef83 | May 5, 2009 | Reply

  4. i was using this code for visual studio 2005. But i came with this error..

    “Cannot implicitly convert type’System.workflow.ComponentModel.DependencyProperty’ to ‘Microsoft.crm.sdk.lookup'”

    hope that could help me in this case…. Thank you…

    Comment by Zosimo Recio | May 26, 2009 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: