Translate

Sunday, 25 December 2016

Generate CRM organization service using service proxy

How to generate CRM organization service using service proxy ?

Hello CRM Developers,
          This is my new post to generate organization service in CRM 2013,2015 and 2016. Simplest method to generate dynamics CRM organization service,you can use below code for creting crm organisation service. For this you need to follow below instructions.

For generation organization service below assemblies are required.
  1. Microsoft.Xrm.Sdk.dll
  2. Microsoft.Crm.Sdk.Proxy.dll
  3. System.configuration
  4. System.ServiceModel
And below name spaces will be required
  1. using Microsoft.Crm.Sdk.Messages;
  2. using Microsoft.Xrm.Sdk;
  3. using Microsoft.Xrm.Sdk.Client;
  4. using System.ServiceModel.Description;

 Add below code in .cs file and App config file respectively. 

//.CS File Code

public static IOrganizationService GetCRMService()
{
   try
   {
      // Get Ogranization Service Url
      Uri oUri = new Uri(System.Configuration.ConfigurationManager.AppSettings["OrganizationUrl"]);
      //Your credentials
      ClientCredentials clientCredentials = new ClientCredentials();
      clientCredentials.UserName.UserName = System.Configuration.ConfigurationManager.AppSettings["Username"];
      clientCredentials.UserName.Password = System.Configuration.ConfigurationManager.AppSettings["Password"];

      //Create your Organization Service Proxy
      OrganizationServiceProxy _serviceProxy = new OrganizationServiceProxy(
         oUri,
         null,
         clientCredentials,
         null);
      _serviceProxy.EnableProxyTypes();

      IOrganizationService service = _serviceProxy;

      // Obtain information about the logged on user from the web service.
      Guid userId =  ((WhoAmIResponse)_serviceProxy.Execute(new WhoAmIRequest())).UserId;
      if (userId != null) 
       { return service; }
      else
       { return null; }
       }
   catch (Exception)
    {
        return null;
    }
}

//App config file code

<?xml version="1.0" encoding="utf-8"?>
<configuration>
   <appSettings>
    <!--Organization Service Endpoint Address-->
    <add key="OrganizationUrl" value="https://your organisation/XRMServices/2011/Organization.svc" />

    <!--UserName with domain-->   
    <add key="Username" value="username" />

    <!--password-->  
    <add key="Password" value="password" />
  </appSettings>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Xrm.Sdk" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
      </dependentAssembly>      
    </assemblyBinding>
  </runtime>
</configuration>
 

Enjoy the great technology Dynamics CRM. Good luck.....!!!!! 
Please feel free to put comments for your queries.Thanks.
Post a Comment