Create WCF service and use it to retreive data from Dynamics CRM

1. Open Visual Studio and Select WCF service application.

2. Open IService1.cs and add following code::

namespace WcfServiceCRM
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    public interface IService1

        Account[] getAccountDetails();

        // TODO: Add your service operations here

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    public class Account
        string fullname;
        string companyname;
        string telephone;

        public string FullName
            get { return fullname; }
            set { fullname = value; }

        public string CompanyName
            get { return companyname; }
            set { companyname = value; }

        public string Telephone
            get { return telephone; }
            set { telephone = value; }

3. Now open Service1.svc.cs and add following code
(Add necessary refernces from CRM sdk for accessing your CRM instance(eg., Microsoft.Crm.Sdk, etc.))::

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

namespace WcfServiceCRM
    public class Service1 : IService1

// Create connection to your CRM instance and get the XML of entity which you want to retreive.
// Here i am using Lead entity.

        OrganizationService _orgservice = new OrganizationService(CrmConnection.Parse("Url=;; Password=Password.;"));
        Account[] IService1.getAccountDetails()
            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));
            int i = 0;
            int Rcount = result.Entities.Count;
            Account[] arrAccount = new Account[Rcount];
            foreach (var c in result.Entities)
                arrAccount[i] = new Account();

                arrAccount[i].FullName = c.Attributes["fullname"].ToString();

                if (c.Attributes.Contains("companyname"))
                    arrAccount[i].CompanyName = c.Attributes["companyname"].ToString();

                if (c.Attributes.Contains("telephone1"))
                    arrAccount[i].Telephone = c.Attributes["telephone1"].ToString();

            return arrAccount;


Consume your WCF Service

5. Now your WCF Service is ready to use. For consuming this service, Open Visual Studio and Create new Web application

6. Put a gridview in your design window(.aspx page)

7. Goto References and Add reference of your crm Service dll. Goto Browse in Add Reference and select the path of your crm srvice dll in bin folder.

8. Goto to the code window and add namespace and add below code for binding data to gridview.::

using YourWcfServiceReference;

namespace CRMdataWCF
    public partial class WebForm1 : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            WcfServiceCRM.IService1 srv= new Service1();
            Account acc = new Account();
            GridView1.DataSource = srv.getAccountDetails();


  1. Thanks a lot!! it's been of great help!!

  2. 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

  3. The great service in this blog and the nice technology is visible in this blog. I am really very happy for the nice approach is visible in this blog and thank you very much for using the nice technology in this blog
    Best CRM System


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