Making connection to organization data

Very often organizations use Dynamics CRM as one of the software components within their integrated IT platform consisting of ERP, web site, human resource management system etc. Therefore, strong integration of Dynamics CRM and its data with other components is necessity for those organizations, meaning that it is required to establish flows of data between systems, ensuring consistency and efficiency at the same time. One of the ways to accomplish that is to enable access to data stored in Dynamics CRM from other systems, so that the data can be retrieved, created, modified and even deleted outside the box – from distanced systems within or outside the organization! This series of blog posts aims to explain possible ways to do so and to tackle all issues that organization’s IT professionals can face, regardless of their proficiency and experience with Dynamics CRM. Posts will be written in a simple way, with the assumption that the audience knows very little about Dynamics CRM and taking into the consideration that the possible readers are organization’s IT professionals who have very little experience in working with Dynamics CRM.

In this blog post, we will present how to connect to Dynamics CRM and how to access its data. We will write C# code that establishes connection towards Dynamics CRM and creates instance of OrganizationService through which it is possible to access organization data. This C# code can be a part of any C# based application. It is assumed that most of applications within the organization that uses Dynamics CRM are actually .NET applications and therefore showing this functionality in C# would be the most useful.

Traditional way in which client applications can reach organization data from Dynamics CRM is through object of OrganizationService class. It is the implementation of IOrganizationService interface which has all methods needed for accessing the data. But, before using it, few essential steps need to be done.

We will set up new Console application project in Visual Studio 2012. It could be any other type of VS project.

Img1

In order to instantiate OrganizationService class, installing Microsoft CRM 2011 SDK and Microsoft CRM 2011 Client packages is required. Right click on solution name in Solution Explorer and then click on Manage NuGet Packages. On the left side choose Online and in right upper corner type “Microsoft CRM 2011” and then install it. Repeat the procedure with “Microsoft CRM 2011 Client”.

Img2

 

All required classes are within the installed packages. It is a good practice to wrap all the code into try-catch block, since methods of OgranizationService can very often generate an exception (i.e. you attempt to create a record of non-existing entity) and you would certainly like to handle it in a right way. First thing that should be done is to establish connection to the specific instance of CRM Online. That is being done by creating an instance of CrmConnection class and passing connection string as a parameter to its constructor. Connection string is a string consisting of the URL of the CRM Online instance, username and password of CRM user whose credentials will be used to connect.

Connection string should look like this:

string con = "Url=URL_OF_CRM; Username=USERNAME; Password=PASSWORD;";

Alternatively, if the project is Web application project, it is a good practice to store connection string in Web Config file like this:

<configuration>
     <connectionStrings>
          <add name=" CRMOnlineO365" connectionString=” Url=URL_OF_CRM; Username=USERNAME; Password=PASSWORD;" />
     </connectionStrings>
</configuration>

It can be retrieved from the Web configuration to the code by the ConfigurationManager in the following manner:

string con = ConfigurationManager.ConnectionStrings["CRMOnlineO365"].ConnectionString;

Now, connection to CRM can be created using the connection string:

Microsoft.Xrm.Client.CrmConnection connection = Microsoft.Xrm.Client.CrmConnection.Parse(con);

And now we can create an object of OrganizationService class. The best practice is to instantiate it with “using” statement, just to ensure that it will be disposed as soon as it leaves the scope.

using (Microsoft.Xrm.Client.Services.OrganizationService orgService = new Microsoft.Xrm.Client.Services.OrganizationService(connection))
{
}

Now we can use our OrganizationService to reach organization data in any way we want. It provides sufficient methods for all kinds of data manipulation:

Img3

In the next blog post, we will discuss each of these methods. Bear in mind that this approach to working with organization data is becoming depreciated with Microsoft Dynamics CRM 2016, but it still presents widely used method for accessing the data and therefore should not be ignored. In the following blog posts, we will discuss alternative and increasingly popular approaches to working with the CRM data in client applications.

Just to wrap-up, here is the complete C# code section that we created in this blog post:

namespace ConsoleApplication1 {
     class Program {
          static void Main(string[] args) {
               try { 
                    string con = "Url=https://URL; Username=USERNAME; Password=PASSWORD;"; Microsoft.Xrm.Client.CrmConnection connection = Microsoft.Xrm.Client.CrmConnection.Parse(con); 
                    using (Microsoft.Xrm.Client.Services.OrganizationService orgService = new Microsoft.Xrm.Client.Services.OrganizationService(connection)) { 
                    } 
               } 
               catch (Exception e) {
                    Console.WriteLine(e.Message); 
               } 
               Console.ReadLine(); 
          } 
     } 
}