Integrating Microsoft Dynamic CRM with .Net Web-api - International Days

International Days

Internationally important Days

Breaking

Home Top Ad

Post Top Ad

Tuesday, 12 January 2016

Integrating Microsoft Dynamic CRM with .Net Web-api


The Microsoft Dynamic CRM is one of the most effective and durable CRM Available now. Some times the organizations are not satisfied with just CRM , in order to make their process much easier, they are forced to create another costume applications like mobile apps and desktop applications even other custom CRMs . But each applications must be synchronized that they all should be work as connected systems in order to attain full use of distributed systems.
Here i am talking about the integration of Microsoft dynamic CRM with Web-api. Because web API's are more common now and use as the middle layer to communicate with different systems like, Mobile apps and other web sites , other CRMs etc.
In order to connect with Dynamic CRM there is a SDK provided by Microsoft which uses the CRM Services to communicate.I have explained below the step by step creation of web-api which retrieve the Data from Dynamic CRM

1. Create a web-api project 




 
  1. install CRM Sdk From NuGet
     
    or you can use
    Install-Package Microsoft.CrmSdk.CoreTools -Version 7.1.1

    command On Package console 
    The Microsoft SDK includes the architechture over view of CRM ,Entity,Services .The SDK will simplify the complexity of Dynamic CRM and which will speed up the develoupment time. The SDK is actually a group of tools which will enable us to extentds the Microsoft dynamic CRM functionalities . You can find more detauils about this in this link about microsoft dynamic CRM SDK https://msdn.microsoft.com/en-us/library/gg695784(v=crm.7).aspx  

  2. Create the entity classes which will be using for bind the CRM data
    also create the 'Request' and 'Response' Classes for pass request and response objects 

     
    1. Give the Dynamic CRM address on web.config
    You can directly give the url with in the code it self and
    <appSettings>
    <add key="CRMServiceUrl" value="http://CRMURL/XRMServices/2011/Organization.svc"/>
    </appSettings>

    5.In the 'HomeController' , change the inherited class to 'ApiController' 
            public class HomeController : ApiController


    6. Create a HttpPost method to retrieve data from CRM server
    copy and paste below code
    [HttpPost]
public AccountResponse getCRMAccounts(AccountRequest objRequest)
{
AccountResponse objResponse = new AccountResponse();
if(objRequest!=null)
{
IOrganizationService service;
ClientCredentials Credentials = new ClientCredentials();
Credentials.UserName.UserName = objRequest.UserName;
Credentials.UserName.Password = objRequest.Password;
Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
string CRMServer = ConfigurationManager.AppSettings["CRMServiceUrl"].ToString();
Uri OrganizationUrl = new Uri(CRMServer);
Uri HomeRealUrl = null;
using (OrganizationServiceProxy Serviceproxy = new OrganizationServiceProxy(OrganizationUrl, HomeRealUrl, Credentials, null))
{
service = (IOrganizationService)Serviceproxy;
}
if (service != null)
{
QueryExpression query = new QueryExpression
{
EntityName = "account",
ColumnSet = new ColumnSet("name",
"telephone1")
};
EntityCollection entities = service.RetrieveMultiple(query);
// Iterate through returned collection.
objResponse.objAccountList = new List<Account>();
foreach (var c in entities.Entities)
{
Account objAccount = new Account();
objAccount.Name = c.Attributes["name"].ToString();
if (c.Attributes.Contains("telephone1"))
objAccount.Phone = c.Attributes["telephone1"].ToString();
objResponse.objAccountList.Add(objAccount);
}
}
}
return objResponse;
}
    7. Build the solution and run it on 'HTTP Requester' Plug in for Firefox

    and click post button then the request will pass to the program
    the below image shows that our program is connected the CRM server
    Once the connection with the CRM server is succeded then we are going to query out the entity the 'account' is the entity name wich we are going get from CRM and specify the name of the colloum which we want to retireve from the CRM entity
    QueryExpression query = new QueryExpression
{
EntityName = "account",
ColumnSet = new ColumnSet("name",
"telephone1")
};

     
      Now We got the response 'Account' List from CRM server and we loop it and create a custom list for our convenience and shows it as output object 

       
CRM Account list is 
Issues I have faced when creating API's are as below

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

This Xrm is not defined even if I had given the CRM sdk reference to resolve this problem just change the target framework version initially my framework version was .NET framework 4.5 and the i have changed it to .NET framework 4.5.2 then the issue will be resolved 



 

 Please leave your valuable comments

1 comment:

  1. The advantage of productivity is again taken over by Microsoft CRM. Here the users avail the advantage of using Outlook plus OWA experience and accompanied with complete CRM access online and offline, along with some Office 365 features like: Yammer, OneDrive, SharePoint and OneNote (notebooks are auto-created and tracked to the CRM record).

    ReplyDelete

Post Bottom Ad

Pages