Freigeben über


Create your custom web service and integrate with CRM 2011 Online

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 (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 = 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

Comments

  • Anonymous
    September 20, 2011
    On which event this webservice code will fire?? Please let me know how to call this webservice on a custom button click from the crm entity??
  • Anonymous
    September 22, 2011
    Hi Vidhu,You can use java-script or jQuery libraries to call a web service on a button click. There are various articles available online. Also using ASP.net Ajax controls you can achieve that. For this you need to script manager and reference the asmx service.
  • Anonymous
    July 28, 2012
    I need to be able to call this web-service from inside the CRM 2011 online plugin. from what I understand, I cannot set service reference; I can either use basicHttp binding or call the webservice but there are some issues. could you be able to elaborate on what is the better approach?
  • Anonymous
    August 07, 2012
    Hi Ray,You can still use this way of calling inside the plugin using Late binding. If you're looking for ways to add service reference, then this is what you can need to look at blogs.msdn.com/.../ssis-2008-integration-with-crm-2011-on-premise.aspx. This post is dealing with WCF way to connect CRM. Let me know if that helps!!
  • Anonymous
    December 04, 2012
    I encountered errors on using Microsoft.Crm.Services.Utility;am i missing an assembly reference? thanks.
  • Anonymous
    December 09, 2012
    Hi mii,If you're getting a assembly reference error, be sure of using the cs file available in SDKsamplecodecshelpercodedeviceIdManager.cs. Let me know if you need more help.Apurv
  • Anonymous
    February 18, 2013
    Hi Apurv,I have so for worked on development for CRM 2011 on-premise version.CRM 2011 is new to me.In case of on-premise we have all infrastructure such as windows server 2008,IIS 7 where we can deploy our own custom wcf services,we can use windows task schedular for scheduling our batch jobs,we can write custom reports using SSRS or write some SSIS batches.What are our options in case of CRM 2011 online version where we have no access to actual server machines?Client prefers online version in case he does not want to maintain any hardware/infrastructure on premise.
  • Anonymous
    February 18, 2013
    Correction @ above post.CRM 2011 online is new to me**....worked on on premise version so far
  • Anonymous
    May 21, 2013
    Hello Apurv, I was looking at way to call the webservice from CRM ONLINE plugin but unable to find an example of late binding means by which i can invoke the webservice. I looked at your other post, but couldn't help. Can you point me? say I have webservice running at http://abc.com/webservice1. How could I reference this inside the plugin(while crm doesn't support service referencing)!
  • Anonymous
    June 22, 2013
    Hi,I have the same case as 'Ray'. I have a plugin in CRM online 2011 that connects to external webservice, this service operates as data access layer (DAL) so that all logic is implemented in the plugin and the plugin accesses the CRM (get, create, update, ...), through the service.The problem is: when the plugin makes the request to the CRM through the web service (for example to get data from CRM), the service takes long time more than 2 mins, and so the plugin throws the timeout exception.On the other hand, if I make the same request from console application the crm returns data in short time.Can anyone help me please??  
  • Anonymous
    September 09, 2013
    Hi, Can help with samples to invoke on mult browser suport?
  • Anonymous
    April 25, 2014
    i got the error , "Value cannot be null. Parameter name: identityProvider"
  • Anonymous
    December 10, 2014
    Hi Apurv,I have tried same as above. But am getting following error. Can you please help me."The authentication endpoint Username was not found on the configured Secure Token Service!"
  • Anonymous
    September 06, 2016
    Create Simple Web Services with Microsoft Dynamics CRM http://itmukilan.blogspot.in/2016/09/create-simple-web-service-with.html
  • Anonymous
    October 27, 2016
    I think the admin of this web page is actually working hard in favor of his website, for the reason that here every stuff is quality based material.
  • Anonymous
    January 13, 2017
    Awesome things here. I am very satisfied to see your article.Thank you so much and I'm looking forward to touch you. Will you kindly drop me a e-mail?
  • Anonymous
    July 14, 2017
    Some genuinely prime content on this site, saved to fav.