Share via


Simplified connection to Microsoft Dynamics CRM 2011

Hello Folks,

After a long time I am coming up with a new post. I have been researching a lot on pushing a new post into my blog. Finally I have decided to write about the simplified connection to Microsoft Dynamics CRM 2011.

If you have downloaded the latest CRM sdk the connection to CRM application is simplified. Now you just have to pass your Connection String parameters in your configuration file. In your code we can leverage the connection string and connect to CRM application. Following are the steps which you need to leverage the simplified connection string.

 1) Add a config file in your custom application with the following values:

 <?xml version="1.0"?> <configuration> <connectionStrings> <!-- Online using Office 365 --> <!-- <add name="Server=CRM Online, organization=contoso, user=someone" connectionString="Url=https://contoso.crm.dynamics.com; Username=someone@contoso.onmicrosoft.com; Password=password;"/> --> <!-- Online using Windows Live ID --> <!-- <add name="Server=CRM Online, organization=contoso, user=someone@hotmail.com" connectionString="Url=https://contoso.crm.dynamics.com; Username=someone@hotmail.com; Password=password; DeviceID=11hfn41bbqrg580vyvoea05abc; DevicePassword=fuqNIlx%e$.l*+ax_#8O4abc;"/>--> <!-- On-premises with provided user credentials --> <!-- <add name="Server=myserver, organization=AdventureWorksCycle, user=administrator" connectionString="Url=https://myserver/AdventureWorksCycle; Domain=mydomain; Username=administrator; Password=password;"/> --> <!-- On-premises using Windows integrated security --> <!--<add name="Server=myserver, organization=AdventureWorksCycle" connectionString="Url=https://myserver/AdventureWorksCycle;"/>--> <!-- On-Premises (IFD) with claims --> <!--<add name="Server=litware.com, organization=contoso, user=someone@litware.com" connectionString="Url=https://contoso.litware.com; Username=someone@litware.com; Password=password;"/>--> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>

2) This connection string can be leveraged either for all types of CRM connections. If you are connecting to CRM online, you need to get the Device ID and Device Password. They can be generated using the Device Registration tool available in your CRM SDK. This tool is available in the tools folder of CRM sdk named "Device Registration". To register your device and generate the Device credentials following is the command Prompt:

deviceregistration.exe /operation:Register

 

3) After passing all the configuration details in your application. You need to leverage them in your code as shown in the below code:

 

  // Obtain connection configuration information for the Microsoft Dynamics
 // CRM organization web service.
 String connectionString = GetServiceConfiguration();
  
 4) Now let us see the GetServiceConfiguration function definition:
 /// <summary>
 /// Gets web service connection information from the app.config file.
 /// If there is more than one available, the user is prompted to select
 /// the desired connection configuration by name.
 /// </summary>
 /// <returns>A string containing web service connection configuration information.</returns>
 private static String GetServiceConfiguration()
 {
 // Get available connection strings from app.config.
 int count = ConfigurationManager.ConnectionStrings.Count;
 
 // Create a filter list of connection strings so that we have a list of valid
 // connection strings for Microsoft Dynamics CRM only.
 List<KeyValuePair<String, String>> filteredConnectionStrings = 
 new List<KeyValuePair<String, String>>();
 
 for (int a = 0; a < count; a++)
 {
 if (isValidConnectionString(ConfigurationManager.ConnectionStrings[a].ConnectionString))
 filteredConnectionStrings.Add
 (new KeyValuePair<string, string>
 (ConfigurationManager.ConnectionStrings[a].Name,
 ConfigurationManager.ConnectionStrings[a].ConnectionString));
 }
 
 // No valid connections strings found. Write out and error message.
 if (filteredConnectionStrings.Count == 0)
 {
 Console.WriteLine("An app.config file containing at least one valid Microsoft Dynamics CRM " +
 "connection string configuration must exist in the run-time folder.");
 Console.WriteLine("\nThere are several commented out example connection strings in " +
 "the provided app.config file. Uncomment one of them and modify the string according " +
 "to your Microsoft Dynamics CRM installation. Then re-run the sample.");
 return null;
 }
 
 // If one valid connection string is found, use that.
 if (filteredConnectionStrings.Count == 1)
 {
 return filteredConnectionStrings[0].Value;
 }
 
 // If more than one valid connection string is found, let the user decide which to use.
 if (filteredConnectionStrings.Count > 1)
 {
 Console.WriteLine("The following connections are available:");
 Console.WriteLine("------------------------------------------------");
 
 for (int i = 0; i < filteredConnectionStrings.Count; i++)
 {
 Console.Write("\n({0}) {1}\t",
 i + 1, filteredConnectionStrings[i].Key);
 }
 
 Console.WriteLine();
 
 Console.Write("\nType the number of the connection to use (1-{0}) [{0}] : ", 
 filteredConnectionStrings.Count);
 String input = Console.ReadLine();
 int configNumber;
 if (input == String.Empty) input = filteredConnectionStrings.Count.ToString();
 if (!Int32.TryParse(input, out configNumber) || configNumber > count || 
 configNumber == 0)
 {
 Console.WriteLine("Option not valid.");
 return null;
 }
 
 return filteredConnectionStrings[configNumber - 1].Value;
 
 }
 return null;
 
 }
 5) This connection string can be leveraged to connect to CRM application. Following is the code to leverage the connection string and establish a connection to CRM.
  // Establish a connection to the organization web service using CrmConnection.
 Microsoft.Xrm.Client.CrmConnection connection = CrmConnection.Parse (connectionString);

 

  6) Now as we have established the connection we can leverage it to create records into CRM application. Following is the code to achieve that:

 private OrganizationService _orgService;
 
 
 // Obtain an organization service proxy.
 // The using statement assures that the service proxy will be properly disposed.
 using (_orgService = new OrganizationService(connection) )
 {
 Entity account = new Entity("account");
 account["name"] = "Contoso";
 _orgService.Create(account);
 }
 
 That is it!!! Now connecting to CRM is quite easy. Enjoy!!!