Create your custom web service and integrate with CRM 2011 Online

2011-8-25 | apurvghai | Dynamics CRM SDK | C#


I hope this article can help you understand when you need to create a middleware between CRM system and a Legacy system/or custom application.The below example talks about the “contact” entity where the web service accepts the “lastname” attribute of it and inserts the same into CRM system. The end result is the guid which is retrieved and confirms the successful execution of the operation.

Before we get started, here’s the list of requirements

  1. Microsoft Visual Studio 2010
  2. MS .NET Framework 4.0
  3. MS CRM SDK 2011

Walkthrough Steps: Web Service

  • Open Visual Studio 2010
  • Select your project, ASP.NET Empty Web Application
  • Select a suitable name of your project
  • On Successful Creation of Project
  • Go to Solution Explorer
  • Add New Item: Web Service


        
 

  • Provide a suitable name
  • Click on Add to create
  • You will see the C# content in “CustomWebService.asmx.cs”
  • Add reference to CRM Assemblies

     

  • Adding another .NET assemblies

 
      

  • Adding Live ID Code File available in CrmSdk (SDKsamplecodecshelpercode)
  • Declare the namespace

        

  • Write the code for CRM

/// <summary>
/// This function accepts the lastname and creates a contact with that in CRM
/// </summary>
/// <param name="lname"></param>
/// <returns></returns>
public string CreateContact(string lname)
{
     string message = string.Empty;
     try
     {
     OrganizationServiceProxy serviceProxy;
     ClientCredentials deviceCredentials = null;
     ClientCredentials clientCredentials = null;
     Guid contactId = Guid.Empty;
    
     //Organization URL
     Uri OrganizationUri = new Uri(String.Format("https://{0}.api.crm.dynamics.com/XRMServices/2011/Organization.svc","<<Organization>>"));
     //Discovery URL
     Uri HomeRealmUri = new Uri(String.Format("https://dev.{0}/XRMServices/2011/Discovery.svc", "crm.dynamics.com"));
     //Setting Client Credentials
     clientCredentials = this.GetCredentials("<<Live Id>>", "<<Live Password>>");
     //Get the Device Id and Password
     deviceCredentials = this.GetDeviceCredentials();
     //Using Organization Service Proxy to instantiate the CRM Web Service
     using (serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, clientCredentials, deviceCredentials))
     {
          // This statement is required to enable early-bound type support.
          serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
          IOrganizationService service = (IOrganizationService)serviceProxy;
          //Define Entity Object
           Entity contact = new Entity("contact");
          //Specify the attributes
          contact.Attributes["lastname"] = lname;
          //Execute the service
          contactId = service.Create(contact);
          //Confirmation Message
           message = "Contact Created with LastName:- " + lname + " Guid is" + contactId.ToString();

      }
     }
     catch (Exception ex)
     {
           message = ex.Message;
     }
     //returns the message
     return message;
}

  • Write the other functions used in the code above

        protected virtual ClientCredentials GetCredentials(string username, string password)
        {
            ClientCredentials credentials = new ClientCredentials();
            credentials.UserName.UserName = username;
            credentials.UserName.Password = password;
            return credentials;
        }

        protected virtual ClientCredentials GetDeviceCredentials()
        {           
          return Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
        }

  • Complie your code
  • You've successfully completed the walkthrough.

Useful links

  1. How to use retrieve method

Recent Posts
Getting started with Dynamics 365 CRM SDK in C#
Jan 14, 19 | Dynamics CRM SDK
Using Postman with Dynamics 365 Online and OnPremise
Jan 14, 19 | Dynamics CRM SDK
Getting Started with WebAPI
Dec 28, 18 | Dynamics CRM SDK