Logo
HOW TO
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Server

Integrating BizTalk Server 2010 and Microsoft Dynamics CRM : Communicating from BizTalk Server to Dynamics CRM (part 5) - Generating a proxy service

3/29/2013 4:40:44 PM

2. Using a Dynamics CRM proxy service with BizTalk Server

Setup

The proxy-service strategy requires the installation of the Dynamics CRM 2011 SDK. Make sure to add the XRM assemblies to the Global Assembly Cache of your development machine. In addition, whether or not you are using Dynamics CRM's new claims-based security strategy, you still need to install the Windows Identity Foundation (WIF) components&; as there is a dependency between the client assemblies and WIF.

Generating a proxy service

The secret to the proxy service is the typed classes that represent the standard and custom entities in the Dynamics CRM application:

  1. Within a Windows command prompt, navigate to the bin directory beneath the CRM SDK download package.

  2. Execute the CrmSvcUtil.exe utility which creates .NET classes for the entities within Dynamics CRM 2011. This utility has a series of command line parameters including:

    1. url — endpoint address of Dynamics CRM Organization service

    2. out — the file name of the generated code

    3. username — name of the user with permission to invoke the Dynamics CRM services

    4. password — password of the user with permission to invoke the Dynamics CRM services

    5. domain — directory domain of the user invoking the Dynamics CRM services

    6. namespace — .NET namespace used in the generated code

    Using these parameters, a sample query to generate the early-bound Dynamics CRM class looks like this:

    CrmSvcUtil.exe /url:http://<server>/<orgName>/XRMServices/2011/Organization.svc /out:"C:\temp\CrmEntites.cs" /username:<username> /password:<password> /domain:<domain> /namespace:Chapter3.CrmProxyService
    
    
    					  
  3. The resulting C# class contains definitions for all of the standard and custom entities present in the target Dynamics CRM system. This class allows us to deal only with typed entities and avoid interacting with the raw Entity format.

  4. In Visual Studio 2010, create a new Web Site project of type WCF Service and name it Chapter3-DynamicsCRM.SvcProxy.

  5. Right-click the new project and choose to Add Existing Item. Locate the CrmEntities.cs class that was generated earlier and include it in this project. Add it to the App_Code folder of the project.

  6. Once again, right-click the project and choose to Add New Item. Select the WCF Service type and name it CrmCustomerProxy.svc.

  7. The contract for this service is displayed, which will contain the data type and operation that BizTalk will invoke. Note that the CrmEntities.cs class does not contain WCF serializable types. Therefore, we cannot simply return the Dynamics CRM "customer" type from our service. Define a new class in ICrmCustomerServiceProxy.cs that describes a customer. Be aware that we are using a single CRM entity, Customer, in this example. A similar exercise would have to be performed for each entity that we wish to expose to BizTalk.

    [DataContract(Namespace="http://chapter3/data")]
    public class Customer
    {
    [DataMember]
    public string NamePrefix { get; set; }
    [DataMember]
    public string FirstName { get; set; }
    [DataMember]
    public string MiddleName { get; set; }
    [DataMember]
    public string LastName { get; set; }
    [DataMember]
    public string PrimaryAddressStreet { get; set; }
    [DataMember]
    public string PrimaryAddressCity { get; set; }
    [DataMember]
    public string PrimaryAddressState { get; set; }
    [DataMember]
    public string PrimaryAddressPostalCode { get; set; }
    [DataMember]
    public string PhoneNumber { get; set; }
    [DataMember]
    public string EmailAddress { get; set; } }
    
    
    					  
  8. Also in this ICrmCustomerProxy.cs file, add the service contract. This contract contains a single operation that takes in a customer object and returns a string containing the new record's unique identifier.

    [ServiceContract(Namespace="http://chapter3/service")]
    public interface ICrmCustomerProxy
    {
    [OperationContract]
    string AddCustomer(Customer newCustomer);
    }
    
  9. Switch to the generated CrmCustomerProxy.cs file and add a placeholder for the required AddCustomer operation.

  10. Before going further, add an assembly reference to this project. Point to the Microsoft.Xrm.Sdk.dll that is included in the bin folder of the Dynamics CRM SDK download.

  11. At the top of the CrmCustomerProxy.cs code class, add three references:

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using System.ServiceModel.Description; //for credentials
    using Chapter3.CrmProxyService; //generated class
    
  12. When calling the Dynamics CRM WCF SOAP service through code, we have the option of using a special service proxy class included in the SDK (OrganizationServiceProxy). By combining this proxy class with a Dynamics CRM WCF behavior (ProxyTypesBehavior), we can work solely with typed objects and let those objects get translated to, and from, Entities underneath the covers. At the beginning of the AddCustomer operation&;, add the following code, which creates a variable for the service proxy and defines the client credentials object:

    OrganizationServiceProxy proxy;
    ClientCredentials creds = new ClientCredentials();
    creds.Windows.ClientCredential =
    System.Net.CredentialCache.DefaultNetworkCredentials;
    
  13. Define the two variables that will hold our Dynamics CRM contact object and the GUID value that is returned when a new contact is created.

    Contact newContact;
    Guid newContactId;
    
  14. We are ready to instantiate the custom service proxy that we used to call the Dynamics CRM service. Notice that our credential object is passed into the constructor. After the proxy is instantiated, add the custom Dynamics CRM WCF behavior to the endpoint:

    using (proxy = new OrganizationServiceProxy(new
    Uri("http://<server>/<instance>/XRMServices/2011/Organization
    .svc"),
    null,
    creds,
    null))
    {
    proxy.ServiceConfiguration.CurrentServiceEndpoint.
    Behaviors.Add(new ProxyTypesBehavior());
    }
    
  15. Create the mapping between the service's customer definition and the Dynamics CRM contact entity:

    newContact = new Contact();
    newContact.Salutation = newCustomer.NamePrefix;
    newContact.FirstName = newCustomer.FirstName;
    newContact.MiddleName = newCustomer.MiddleName;
    newContact.LastName = newCustomer.LastName;
    newContact.Address1_Line1 = newCustomer.PrimaryAddressStreet;
    newContact.Address1_City = newCustomer.PrimaryAddressCity;
    newContact.Address1_StateOrProvince =
    newCustomer.PrimaryAddressState;
    newContact.Address1_PostalCode =
    newCustomer.PrimaryAddressPostalCode;
    newContact.Telephone1 = newCustomer.PhoneNumber;
    newContact.EMailAddress1 = newCustomer.EmailAddress;
    
  16. Finally, invoke the service, pass in the Dynamics CRM entity, and get the GUID response. Return the string value of the GUID from the service operation:

    newContactId = proxy.Create(newContact);
    
  17. Build the service and deploy it to a local web server. Pull up the service in the browser to ensure that it is configured correctly:

  18. If you choose, you may also try and call this service using the WCF Test Client. Make sure that the service sits within an Application Pool that runs with a domain account that has access to Dynamics CRM 2011.

We now have a complete, working proxy service that sits as a façade in front of the actual Dynamics CRM 2011 WCF endpoint. This proxy exposes a clean, typed interface that BizTalk can readily consume, as we will see next.
Other -----------------
- Extending Dynamics AX 2009 (part 3) - Creating Labels, Adding Content to the Wizard
- Extending Dynamics AX 2009 (part 2) - Creating a New Wizard
- Extending Dynamics AX 2009 (part 1)
- System Center Configuration Manager 2007 : Operating System Deployment - Native Mode
- System Center Configuration Manager 2007 : Operating System Deployment - Post Deployment Tasks, Troubleshooting
- System Center Configuration Manager 2007 : Operating System Deployment - Drivers
- System Center Configuration Manager 2007 : Operating System Deployment - Tips and Techniques
- Understanding Network Services and Active Directory Domain Controller Placement for Exchange Server 2007 : Global Catalog and Domain Controller Placement
- Understanding Network Services and Active Directory Domain Controller Placement for Exchange Server 2007 : Configuring DNS to Support Exchange Servers, Troubleshooting DNS Problems
- Understanding Network Services and Active Directory Domain Controller Placement for Exchange Server 2007 : Understanding DNS Requirements for Exchange Server 2007
 
 
REVIEW
- First look: Apple Watch

- 10 Amazing Tools You Should Be Using with Dropbox
 
VIDEO TUTORIAL
- 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