Microsoft Dynamics CRM 2011

Microsoft Dynamics CRM 2011

Tuesday, November 12, 2013

How to Integrate Dynamics CRM with SSIS

by Carmel Schvartzman

  1. In this walkthrough we will learn Step-By-Step How to integrate CRM2011 with SSIS through a Web Service in Dynamics CRM 2011 and Sql Server Integration Services.
    We'll call the CRM 2011 WCF Web Service and perform CRUD operations using Indexing on the generic Entity class. We'll consume the CRM WCF Organization (SOAP) service from a class which we will use from inside an SSIS project, but the same logic can also be used by a console application, or a Windows Service, or a Windows Form application.
    We'll not use the early binding Microsoft CRM SDK 2011; instead we'll reach directly the CRM 2011 WCF SOAP endpoint using Late Binding to perform the required CRUD operations.
  2. Sql Server Integration Services as implemented through Visual Studio 2008, support only the Framework 3.5 libraries at most. Therefore, we cannot use the CRM SDK and its framework 4.0  assemblies while coding from SSIS 2008. Therefore , we'll reach Crm2011 from SSIS via its Organization Web Service.
  3. First, we'll create a .dll which will send requests to the CRM2011 Organization Web Service . Then, we'll create an SSIS project to use that assembly and fetch the CRM data, and also create new entities from SSIS.
  4. So let's create a new Class Project in Visual Studio 2010 or 2008, selecting the target framework to be version 3.5:
    How to Integrate Dynamics CRM with SSIS
  5. Next, add the CRM 2011 Web Service to the project:
  6. Remember that we'll be using the Organization CRM Web Service:
  7. After adding the Web Service, rename the Class to CrmInit:
  8. Create an static method returning an IOrganizationService object. This function will get the host, organization and user logon data:
  9. Now add the following security standard code for reaching the CRM Web Service :





    SymmetricSecurityBindingElement security = new SymmetricSecurityBindingElement();
    security.ProtectionTokenParameters = new SspiSecurityTokenParameters();
    HttpTransportBindingElement httpTransport = new HttpTransportBindingElement();
    httpTransport.MaxReceivedMessageSize = Int32.MaxValue ;
    CustomBinding binding = new CustomBinding();
    binding.Elements.Add(security);
    TextMessageEncodingBindingElement encoding =
    new TextMessageEncodingBindingElement(MessageVersion.Soap12WSAddressing10, Encoding.UTF8);
    binding.Elements.Add(encoding);
    binding.Elements.Add(httpTransport);
  10. Now append the WCF Endpoint code:



    EndpointAddress endpoint =
    new EndpointAddress(new Uri(string.Format("{0}/{1}/XRMServices/2011/Organization.svc", host, organization)),
    EndpointIdentity.CreateUpnIdentity(string.Format("{0}@{1}", user, "")), new AddressHeader[] { });



  11. And finally add the code to craete the Client:





    OrganizationServiceClient client = new OrganizationServiceClient(binding, endpoint);
    client.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(user, password, "");
  12. Check that you have all the "usings" you need:
  13. Also, check the references:
  14. Now, in order to test our class, create a new console Project , which will call our CRM assembly:
  15. Add the following references:
  16. First, add the Serialization assembly:
  17. Second, add our assembly:
  18. Now, code a call to the IOrganizationService:
  19. Add the corresponding using:
  20. ... and type the relevant logon data:
  21. Next, select some columns you want to retrieve, and get the entity object:
  22. Check in the QuickWatch window that you get an account with the required attributes:


  23. Now we want to use the dll, so get to the BIN folder, and get it:
  24. .. and paste it to some relevant folder where you'll keep it:


    Also, paste it to the SSIS folder (if you decide to put the assembly inside the GAC, you could skip this step):
    C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn
  25. Now it's time to create the SSIS project. Open Visual Studio 2008, and create the project:
  26. Search in the toolbox for an Script Task:
  27. Drop the task and rename it:
  28. Change the project properties:
  29. .. in order to run the debbugger in 32 bits mode:


  30. Now open the editor of the task:
  31. Go to the Properties window:
  32. ... and change the target Framework to 3.5:
  33. Now go to the references tab:
  34. ... and add a reference to the Serialization assembly:
  35. ... and to our CRM assembly:
  36. Add the "using" directive to make visible the assembly's classes:
  37. Now go to the CRMProxy project, and copy the code we tested inside our Console project:
  38. Return to the SSIS project and paste the code inside the Main function:
  39. .. and add a breakpoint:
  40. Build the Script Task :
  41. Close the VisualStudio Tool For Applications (VSTA) project, and save the Script Task:
  42. Debug the SSIS project until the breakpoint is reached:
  43. When the debugger reaches the breakpoint, check for the account data returned from the Web Service:
  44. We got the CRM data. Let's now retrieve the Account name:
  45. Run again the debugger, and check for the returned Account name:
  46. It seems OK. We fetch CRM2011 data from the SSIS. Now let's create a new Entity record from the SSIS. Add the following code to create a new Competitor record:
  47. Run the SSIS, and open your CRM2011 to see the new added Competitor:






    This late Entity creation using the "Attributes" ( [""] ) feature, is the result of extending the Entity class to allow for string indexing using the field names. We extended the Entity class in this walkthrough.

    That's all...Enjoy Dynamics CRM


    כתב: כרמל שוורצמן

3 comments:

  1. This works fine over HTTP, but does not work over HTTPS. There appears to be more involved than just changing the binding element to HttpsTransportBindingElement. I receive the following error:
    An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.

    The error only occurs over HTTPS and not HTTP, so it isn't the usual issue with a discrepancy in the client/server time that normally causes this error.

    Have you gotten this to work over HTTPS and what was required?

    ReplyDelete
  2. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well.
    CRM Software in Dubai
    CRM Software
    CRM Software in UAE
    Best CRM Software
    CRM Software for Small Business

    ReplyDelete