Monday, September 23, 2013

Proxy web service to connect to Dynamics CRM

While Dynamics CRM 2011 webservice endpoints are available for theoretically any client, there seems to be a few challenges while it comes to a Java implementation especially using NTLM/ Kerberos authentication. One easy workaround is to create a custom webservice that can then be hosted in IIS which will act as a prxy between the java client and Dynamics CRM. In this post, I will walk through the creation of one such simple webservice which will create a case in CRM.

 1. Create a new WCF Service library project in Visual Studio.
























2. Add a new WCF Service item to the project. Delete theIservice1.cs and Service1.cs files that come with the project.

























3. Modify the ICRMService.cs file to create a new method called CreateCase.











4. Add the following code under CRMService.cs file.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.Xrm.Sdk.Client;
using System.Net;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System.ServiceModel.Description;

namespace DynCRMProxy
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CRMService" in both code and config file together.
    public class CRMService : ICRMService
    {
        public string CreateCase(string contactName, int caseType, string caseDescription)
        {
            string caseId = CreateDynamicsCase(contactName, caseType, caseDescription);
            return caseId;
        }

        private string CreateDynamicsCase(string contactName, int caseType, string Description)//FnAction, string userName)
        {

            string caseId = "";

            ClientCredentials Credentials = new ClientCredentials();

            Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
 

            //This URL needs to be updated to match the servername and Organization for the environment.
            string serverName = "server_Name";
            string orgName = "org_Name";

            Uri OrganizationUri = new Uri("http://" + serverName + "/" + orgName + "/XRMServices/2011/Organization.svc");

            Uri HomeRealmUri = null;

            //OrganizationServiceProxy serviceProxy;       
            try
            {
                using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null))
                {

                    string contactId = "";
                    IOrganizationService service = (IOrganizationService)serviceProxy;

                    QueryByAttribute querybyexpression = new QueryByAttribute("contact");
                    querybyexpression.ColumnSet = new ColumnSet("contactid");
                    //  Attribute to query
                    querybyexpression.Attributes.AddRange("fullname");
                    //  Value of queried attribute to return
                    querybyexpression.Values.AddRange(contactName);

                    //  Query passed to the service proxy
                    EntityCollection retrieved = service.RetrieveMultiple(querybyexpression);

                    //  Iterate through returned collection
                    foreach (var c in retrieved.Entities)
                    {

                        contactId = c.Attributes["contactid"].ToString();
                    }


                    Guid cId = new Guid(contactId);

                    Entity ticket = new Entity("incident");

                    ticket.Attributes["customerid"] = new EntityReference("contact", cId);
                    ticket.Attributes["casetypecode"] = new OptionSetValue(caseType); //other question
                    Guid newCase = service.Create(ticket);
                    caseId = newCase.ToString();

                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error when creating Case " + ex);
            }
            return caseId;
        }
    }
}

If you look at the code, it is a straightforward CRM case creation with the values of Contact and case type set by incoming parameters. I can now test the webserivce via the easy to use built in client by selecting Start -Debug, which hosts the service in Visual Studio. Enter the required values and select the Invoke button - the response should have the guid of the case created.
























Once you are able to build the project without errors and test the webservice calls, the proxy webservice can be hosted in any of the ways detailed in this msdn article:
http://msdn.microsoft.com/en-us/library/ms730158.aspx

Notice that there are some config changes that need to be done if you are going the IIS way.

No comments:

Post a Comment