Connect to Microsoft Dynamics CRM 2013 online using C#

In this blog, I am going to explain how to create connection with Online Microsoft Dynamics CRM and how to fetch data of CRM using C#.

Fetch records from online crm entity using c#. First of all add required references and add few important namespaces :

using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Messages;

Now, for creating connection with Online dynamics CRM 2013, add below code inside your class with your URL, UserName and Password of your Online Dynamics CRM .

OrganizationService _orgservice = new OrganizationService(CrmConnection.Parse("Url=;; Password=YourPassword;"));

Now, for retrieving records from your online CRM for any entity get the fetchXML by using AdvancedFind and use that fetchXML here for getting records. Here, in my example i am fetching records of Lead entity. ::

string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
  <entity name='lead'>
    <attribute name='fullname' />
    <attribute name='companyname' />
    <attribute name='telephone1' />
    <attribute name='leadid' />
    <attribute name='statecode' />
    <order attribute='fullname' descending='false' />
    <filter type='and'>
      <condition attribute='statecode' operator='in'>

                 EntityCollection result = _orgservice.RetrieveMultiple(new FetchExpression(fetchXML));
                foreach (var c in result.Entities)
                    Label2.Text = "Total records retrieved through fetchXml are: " + ListBox2.Items.Count.ToString();
                   // Console.WriteLine("Type of enquiry: {0} , Enquiry No.: {1}", c.Attributes["new_keytypeofenquiryid"].ToString(), c.Attributes["leadid"].ToString());
                catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
                // You can handle an exception here or pass it back to the calling method.


  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Microsoft CRM, kindly contact us
    MaxMunus Offer World Class Virtual Instructor led training on Microsoft CRM. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Nitesh Kumar
    Skype id: nitesh_maxmunus
    Ph:(+91) 8553912023


Post a Comment

Popular posts from this blog

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

Dumps for Microsoft Dynamics CRM MB2-703 Practice Exam Questions Free

How to import CSV files into DataTable in C#

How to show enlarge image when mouse hover on image or link in Asp.Net(c#) using JavaScript

go to top image