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.
[ServiceContract]
public interface IService1
{
[OperationContract]
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.
[DataContract]
public class Account
{
string fullname;
string companyname;
string telephone;
[DataMember]
public string FullName
{
get { return fullname; }
set { fullname = value; }
}
[DataMember]
public string CompanyName
{
get { return companyname; }
set { companyname = value; }
}
[DataMember]
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=https://YourSolution.crm5.dynamics.com; Username=userName@YourSolution.onmicrosoft.com; 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'>
<value>0</value>
<value>1</value>
</condition>
</filter>
</entity>
</fetch>";
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();
i++;
}
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();
GridView1.DataBind();
}
}
}
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.
[ServiceContract]
public interface IService1
{
[OperationContract]
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.
[DataContract]
public class Account
{
string fullname;
string companyname;
string telephone;
[DataMember]
public string FullName
{
get { return fullname; }
set { fullname = value; }
}
[DataMember]
public string CompanyName
{
get { return companyname; }
set { companyname = value; }
}
[DataMember]
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=https://YourSolution.crm5.dynamics.com; Username=userName@YourSolution.onmicrosoft.com; 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'>
<value>0</value>
<value>1</value>
</condition>
</filter>
</entity>
</fetch>";
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();
i++;
}
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();
GridView1.DataBind();
}
}
}
Thanks a lot!! it's been of great help!!
ReplyDeleteI 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 http://www.maxmunus.com/contact
ReplyDeleteMaxMunus 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
MaxMunus
E-mail: nitesh@maxmunus.com
Skype id: nitesh_maxmunus
Ph:(+91) 8553912023
http://www.maxmunus.com/
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
ReplyDeleteBest CRM System