HTML Dropdown

Tuesday, 26 July 2016

Retrieve Contact Records from CRM using LINQ


1. Generate early bound types

Run the CrmSvcUtil.exe tool, with the “Microsoft.Xrm.Client.CodeGeneration” extension, to generate your entity classes and service contexts. The following is an example command to create a file called Xrm.cs that points at an instance of Microsoft Dynamics CRM. Note that the Microsoft.Xrm.Client.CodeGeneration.dll file must be in the same directory as the CrmSvcUtil.exe file, or in the system global assembly cache, when you run this command. The first command shown is for an on-premises organization. The second command is for a CRM Online organization. Both commands should be executed as a single command line with no line breaks.

CrmSvcUtil.exe

/codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration"

/out:Xrm.cs /url:https://<mydomain>.api.crm.dynamics.com/XRMServices/2011/Organization.svc

/username:<myusername>@<mydomain>.onmicrosoft.com /password:<mypassword> /namespace:Xrm /serviceContextName:XrmServiceContext

 

2. Create a Console Application project in Visual studio 2012.


3. Add the following references from the SDK\bin folder and from .NET


Also add the EarlyBound.cs File in your console application that is generated in step1.

4. Add the following using statements in your Program.cs File.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Xml.Linq;

using System.Configuration;

using System.Data;

using System.Text;

using System.ServiceModel;

using System.ServiceModel.Description;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Client;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Sdk.Discovery;

using EarlyBound;

5. Make sure the console project specifies .NET Framework 4.5.2 as the target framework and not .NET Framework 4.5.2 Client Profile. Check the properties of the project by selecting the ConsoleApplication1 project in Solution Explorer and clicking Alt-Enter.

Right Click ConsoleApplication1 -> Properties

 


 

6. Declare the Main Method as below

namespace RetrieveRecords_Early_bound

    {

    class Program

        {

 

         static IOrganizationService _service;

         static void Main(string[] args)

            {

 

            ConnectToMSCRM("UserName", "Password", "Servername");

                Guid userid = ((WhoAmIResponse)_service.Execute(new WhoAmIRequest())).UserId;

                if (userid == Guid.Empty)

                    {

                    return; // Check whether connection established or not

                    }

                retrieveContactRecords();

                Console.ReadLine();

 

            }

         public static void ConnectToMSCRM(string UserName, string Password, string SoapOrgServiceUri)

            {

              // for stablishing the connection with CRM

                try

                    {

                        ClientCredentials credentials = new ClientCredentials();

                        credentials.UserName.UserName = UserName;

                        credentials.UserName.Password = Password;

                        Uri serviceUri = new Uri(SoapOrgServiceUri);

                        OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, credentials, null);

                        proxy.EnableProxyTypes();

                        _service = (IOrganizationService)proxy;

                    }

                catch (Exception ex)

                    {

                        Console.WriteLine("Error while connecting to CRM " + ex.Message);

                        Console.ReadKey();

                    }

            }

         public static void retrieveContactRecords()

             {

              // For Retrieve the records using LINQ

               using (XrmServiceContext context = new XrmServiceContext(_service))

                 {

                     var getContactRecords = from conSet in context.ContactSet

                                             where conSet.StateCode == 0   //Check for Status == Active

                                             orderby conSet.FullName

                                             select conSet;

 

                     if (getContactRecords != null)

                         {

                                 StringBuilder fullNameString = new StringBuilder();

                                 Console.WriteLine("Full Name\n--------------------------");

                                 foreach (var item in getContactRecords)

                                     {

                                         if (item.FullName != null)

                                             fullNameString.AppendLine(item.FullName);

                                     }

 

                                 Console.Write(fullNameString);

                                 Console.ReadKey();

                         }

                 }

             }

 

        }

    }

 

7. Testing

Compile the run the console application


 

8. Testing

Go to CRM and test like below:-


9. Testing

Click on Results