Sunday, June 14, 2015

Create your custom web service and integrate with CRM 2011 Online

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 (SDK\samplecode\cs\helpercode)
  • 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 = newUri(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(newProxyTypesBehavior());
          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()
        {           
          returnMicrosoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
        }
  • Complie your code
  • You've successfully completed the walkthrough.

No comments:

Post a Comment

How to Deploy your API as a web app or API app

  Before you can call your custom API from a logic app workflow, deploy your API as a web app or API app to Azure App Service. To make your ...