Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
Windows Server

Integrating BizTalk Server 2010 and Microsoft Dynamics CRM : Communicating from Dynamics CRM to BizTalk Server (part 2) - Writing the Dynamics CRM plugin

4/19/2013 6:48:53 PM
Writing the Dynamics CRM plugin

A Dynamics CRM plugin is a powerful way to extend the out-of-the-box behavior of the product with custom code. The plugin that we are building for this exercise invokes the service we built above.

Before starting, ensure that you have downloaded the Dynamics CRM 2011 SDK and installed the microsoft.xrm.sdk.dll assembly in the Global Assembly Cache.

  1. In the existing Chapter3-DynamicsCRM Visual Studio 2010 solution, add a new Class Library project named Chapter3-DynamicsCRM.AcctPlugin to the solution.

  2. Add a new reference to this project and point to the microsoft.xrm.sdk.dll assembly. This assembly contains the interfaces and types that we need to correctly define a plugin.

  3. Now, we need a service reference to our BizTalk-generated WCF endpoint. Choose to add a new Service Reference to the project. Point to our previously created service and set the namespace to AcctChangeSvc:

  4. Add a new class file named AccountEventPlugin.cs to the project.

  5. At the top of the new class, add two additional "using" statements pointing to Microsoft.Xrm.Sdk and System.ServiceModel. These assemblies have all that we need to define the plugin and consume our BizTalk-generated service.

  6. The public class should implement the IPlugin interface, which has a single required operation, named Execute:

    public class AccountEventPlugin : IPlugin
    public void Execute(IServiceProvider serviceProvider)
  7. Depending on your use case, you may need to store the result of a successful plugin invocation on a Dynamics CRM record. In this scenario, we will simply log details of our plugin execution to the machine's Event Log. Hence the first line in the Execute operation is as follows:

    WriteEntry("Application", "Plugin invoked successfully", System.
  8. Next up, we need to acquire context about the data entity being passed to the plugin. The IPipelineExecutionContext holds such information in addition to the runtime environment that the plugin is executing in:

    IPluginExecutionContext context = (IPluginExecutionContext)
  9. We will be extracting data from an Account, and thus need a variable that references this entity. At the time of declaring this variable, its value is null:

    Entity accountEntity = null;
  10. Because one can use a plugin in all sorts of scenarios, some preventative error checking is prudent. To start with, we can ensure that the object that was passed into the plugin is indeed a Dynamics CRM entity. The context object defined earlier contains a set of input parameters containing the data from the request message:

    if (context.InputParameters.Contains("Target") && context.
    InputParameters["Target"] is Entity)
  11. If the plugin target is an entity, set the previously defined accountEntity variable&; to the entity passed into the plugin:

    //retrieve entity from input params
    accountEntity = (Entity)context.InputParameters["Target"];
  12. We could have tied this plugin to any Dynamics CRM entity and therefore should check and make sure that the type of entity passed into the plugin is valid:

    //if the target account type isn't "account" exit
    if (accountEntity.LogicalName != "account")
    "Target is not 'account' type",
  13. One key thing to realise is that when the "Update" events occur in the Dynamics CRM event pipeline, only the changed fields of an entity are put in the context's property bag. In order to have access to the entire payload of the account entity, we can use an Image. An Image is a representation of an entity either before or after it was saved to Dynamics CRM. There are four fields of the Account entity that we are interested in and those can be retrieved from the PostEntityImages collection that is part of the context. We use the name "PostEventImage" here and will refer to it later when we register this plugin with Dynamics CRM:

    Entity acctImage = context.PostEntityImages["PostEventImage"];
    string acctName = accountEntity["name"].ToString();
    string acctNumber = accountEntity["accountnumber"].ToString();
    string state = accountEntity["address1_stateorprovince"].ToString();
    string phone = accountEntity["telephone1"].ToString();
  14. Armed with the necessary data elements, we can now create the WCF service input object. The AccountChangeEvent objec&;t is defined as part of the Service Reference established previously:

    AcctChangeSvc.AccountChangeEvent acct = new AcctChangeSvc.AccountChangeEvent();
    acct.EventSource = "Dynamics CRM";
    acct.AccountName = acctName;
    acct.AccountNumber = acctNumber;
    acct.PrimaryState = state;
    acct.PrimaryPhoneNumber = phone;
  15. We are now ready to invoke the service from code. First, create a reference to the binding type associated with our BizTalk-generated service. In this example, use the BasicHttpBinding without any security settings turned on:

    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
  16. Next, an endpoint address is required. Put in the full service URL associated with your BizTalk endpoint:

    EndpointAddress addr = new EndpointAddress("http://localhost/AccountChangeService/AccountChangeService.svc");
  17. This information is all that is needed to instantiate the service client that was defined in our Service Reference:

    AcctChangeSvc.AccountChangeServiceClient client = new AcctChangeSvc.AccountChangeServiceClient(binding, addr);
  18. Invoke the PublishAccountChange operation and upon success, write a message to the machine's Event Log:

    client.PublishAccountChange(acct); System.Diagnostics.EventLog.WriteEntry("Application",
    "Service called successfully",

The plugin is now ready for deployment and registration in the Dynamics CRM environment.

Other -----------------
- SharePoint 2010 : Farm Governance - Configuring a Managed account
- SharePoint 2010 : Farm Governance - Administering SharePoint Designer
- SQL Server 2008 R2 : Creating and Managing Stored Procedures - Debugging Stored Procedures Using SQL Server Management Studio
- SQL Server 2008 R2 : Creating and Managing Stored Procedures - Using Output Parameters, Returning Procedure Status
- Windows Server 2008 : Designing the Active Directory Administrative Model (part 3) - Planning to Audit AD DS and Group Policy Compliance, Planning Organizational Structure
- Windows Server 2008 : Designing the Active Directory Administrative Model (part 2) - Using Group Strategy to Delegate Management Tasks
- Windows Server 2008 : Designing the Active Directory Administrative Model (part 1) - Delegating Active Directory Administration
- BizTalk Server 2006 : Starting a New BizTalk Project - Organizing Artifacts in BizTalk 2006
- BizTalk Server 2006 : Starting a New BizTalk Project - Structuring and Integrating with Visual Studio
- Deploying the Client for Microsoft Exchange Server 2007 : Planning Considerations and Best Practices, Preparing the Deployment
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
Top 10
- Microsoft Excel : How to Use the VLookUp Function
- Fix and Tweak Graphics and Video (part 3) : How to Fix : My Screen Is Sluggish - Adjust Hardware Acceleration
- Fix and Tweak Graphics and Video (part 2) : How to Fix : Text on My Screen Is Too Small
- Fix and Tweak Graphics and Video (part 1) : How to Fix : Adjust the Resolution
- Windows Phone 8 Apps : Camera (part 4) - Adjusting Video Settings, Using the Video Light
- Windows Phone 8 Apps : Camera (part 3) - Using the Front Camera, Activating Video Mode
- Windows Phone 8 Apps : Camera (part 2) - Controlling the Camera’s Flash, Changing the Camera’s Behavior with Lenses
- Windows Phone 8 Apps : Camera (part 1) - Adjusting Photo Settings
- MDT's Client Wizard : Package Properties
- MDT's Client Wizard : Driver Properties
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro