Calling .NET assemblies from Workflow - Part 2. Level 200
NOTE: Before doing this lab you should complete the first HOL on .NET Assemblies:
http://blogs.msdn.com/jstraumann/archive/2006/01/18/514686.aspx
So as a next step, this example shows how to read the GUID of the calling object from MSCRM when calling a .NET assembly from workflow. So for example if a workflow fires on the Account create event, the workflow can call a .NET assembly which can read the GUID of the Account that fired the workflow rule. So here's how to do it and thankfully it's fairly easy.
So first, you can either open up the project you created for the first part
of the lab, or open up VS 2003 ** *NOTE* .NET Assemblies for MSCRM *must*
be written and built with VS 2003, not 2005! **. If creating a new project you
need to select new Project->ClassLibrary.
Once the project is open, we need to add a reference to the MSCRM web reference
as described in the SDK.
To use the Microsoft CRM Web services you need to add a Web reference to your solution. The following Web services are needed for development with this SDK:
http://<your server name>/mscrmservices/2006/crmservice.asmx
http://<your server name>/mscrmservices/2006/metadataservice.asmx
Adding a Web Reference In Visual Studio
http://<your server name>/mscrmservices/2006/crmservice.asmx
The Microsoft CRM Web service is now added to your project. In your source code you can refer to the classes available in this SDK.
So after you name your Web Reference you should add a "using" statement to your code. So for example, my reference is named "mscrm" and I add a suing statement as shown below.

The next step is to create our new method. I have kept things very simple here, and all my method does is return the GUID passed to it:

So when you are done with your code it should be similar to what is shown above. Note that my namespace is "ClassLibrary1", and my class is called "Class1". So that's it for code, nice and simple!
Build the project, and you should end up with a dll with the namespace name, so I get a dll called ClassLibrary1.dll. Copy your dll to \Program Files\Microsoft CRM\Server\Bin\Assembly
The next step is we have to tell the workflow engine that we are adding a new assembly. In that same directory where we just copied the assembly is a file called workflow.config. This is the file we need to add the definition for our new method to. If you've not done this before I would strongly suggest you look at the section in the SDK entitled "Workflow .NET Assemblies". The code from my workflow.config file is shown below.
<method name="returnGuid"
assembly="ClassLibrary1.dll"
typename="ClassLibrary1.Class1"
methodname="retGuid"
timeout="7200">
<parameter name="Caller" datatype="caller"/>
<parameter name="accountId" datatype="lookup" entityname="account"/>
<result datatype="string"/>
</method>
Note that the method name in the first line can be whatever you want it to be.
the method name in line 4 *must* match the method name in your .Net code.
So changing the names to match your project, add the method definition to your workflow.config file and save the file. The open a command prompt and issue an iisreset. Last, restart the workflow service and you should be good to go.
Next we need to write a workflow to call the assembly. We are going to create a very simple workflow that sets the value of a data filed to the GUID of the calling object. Open workflow manager and create a new Account workflow rule on a "manual" event.


Next click insert Action->Call Assembly and choose your assembly name:

You should see the screen shown below. Give your assembly a name, and then double-click on the "accountid" parameter line.

Note that if you used different names your screen will be different.
So you should now see the screen shown below. Select Dynamic Value and then
"Account" This will pass the Account object to your assembly.

Click OK and you should see this screen:

Click OK and you'll see:

Next, we'll set one of the fields on the form to the GUID of the calling object. Pick a field you know is on the form for this, I chose Address1:Fax.
Click Insert Action->Update Entity. Make sure Account is selected and choose the field you want to update. In the middle box choose "=" and then click the lookup.

On this screen choose "Dynamic Value" and then from the drop-down choose whatever name you gave your Assembly in step 1.

Click OK, and you should be back at the screen as shown below.

And we are done. Click "Save" and don't forget to activate the rule!
Open MSCRM and click Sales->Accounts. Select an Account and then click More Actions->Apply Rule

Next Choose the workflow rule we just created.

When it's done, open the account and the GUID should be in whatever field you specified.

Please note that all postings and code samples on this site are provided "AS IS" with no warranties, and confers no rights. Use of any included script or code samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm