Monday, 29 June 2015

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

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



1. Open Visual Studio and Select WCF service application.




2. add following code to the IService1.cs file 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

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

        [OperationContract]
        SampleTestEntity[] getAccountDetails();
     }

    [DataContract]
    public class SampleTestEntity
    {
        string new_sample_name;
        string new_gender;
        string new_contactnumber;

        [DataMember]
        public string sample_name
        {
            get { return new_sample_name; }
            set { new_sample_name = value; }
        }

        [DataMember]
        public string gender
        {
            get { return new_gender; }
            set { new_gender = value; }
        }

        [DataMember]
        public string contactnumber
        {
            get { return new_contactnumber; }
            set { new_contactnumber = 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.Crm.Sdk;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Sdk.Query;

namespace CrmWcfService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {


        OrganizationService _orgservice = new OrganizationService(CrmConnection.Parse("Url=https://YourSolution.crm5.dynamics.com; Username=userName@YourSolution.onmicrosoft.com; Password=Password.;"));



        public SampleTestEntity[] getAccountDetails()
        {
            string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                                  <entity name='new_SampleTestEntity'>
                                    <attribute name='new_sample_name' />
                                    <attribute name='new_gender' />
                                    <attribute name='new_contactnumber' />
                                    <attribute name='new_birthdate' />
                                    <attribute name='new_address' />
                                    <attribute name='new_SampleTestEntityid'/>    
                                    <order attribute='fullname' descending='false' />
                                    <filter type='and'>
                                      <condition attribute='new_sample_name' operator='eq' value='pallavi' />
                                      </condition>
                                    </filter>
                                  </entity>
                                </fetch>";

            EntityCollection result = _orgservice.RetrieveMultiple(new FetchExpression(fetchXML));
            int i = 0;
            int Rcount = result.Entities.Count;
            SampleTestEntity[] arrAccount = new SampleTestEntity[Rcount];
            foreach (var c in result.Entities)
            {
                arrAccount[i] = new SampleTestEntity();

                arrAccount[i].sample_name = c.Attributes["new_sample_name"].ToString();

                if (c.Attributes.Contains("new_gender"))
                    arrAccount[i].contactnumber = c.Attributes["new_gender"].ToString();

                i++;
            }
            return arrAccount;
        }

        //public string GetData(int value)
        //{
        //    return string.Format("You entered: {0}", value);
        //}

        //public CompositeType GetDataUsingDataContract(CompositeType composite)
        //{
        //    if (composite == null)
        //    {
        //        throw new ArgumentNullException("composite");
        //    }
        //    if (composite.BoolValue)
        //    {
        //        composite.StringValue += "Suffix";
        //    }
        //    return composite;
        //}
        
    }
}


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 System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wcfcrmservice;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Service1Client obj = new Service1Client();
        SampleTestEntity acc = new SampleTestEntity();
        GridView1.DataSource = obj.getAccountDetails();
        GridView1.DataBind();
    }
}



































No comments:

Post a Comment