Thursday, September 29, 2011

CRM online 2011 with Odata and WCF

Hi guys, I have strugled a lot with working on CRM 2011 online. It has killed my two day's. Oh!!  It might be beacause of my low brain or subject :) :)
Any how I am able to manage to get data from CRM 2011 online.  In my previous blog, I have explained about basics about CRM 2011.

Now, I am going to explain how do we access CRM 2011 online data using services.

CRM 2011 Online


1. Register for CRM 2011 Online using Windows Live ID.
You can get it here: http://crm.dynamics.com/en-us/home

Available services in CRM 2011.

CRM can allow multiple Organizations to be created on 1 CRM installation.

Each Organization can be for a different company/purpose. So, users may want to access one of the Organizations on CRM installation. If the user/code already knows the Organization URL OR Organization Service URL then fine he can directly use organization service.

If not known, then he needs to write code to connect Discovery Service URL, then browse through all Organizations available and get Organization service URL for respective/required Organization.
In short, whatever Data/Metadata operations we want to do, we will need to use Organization Service URL.
If we want to find Organization Service URL for an Organization, then we need to do that using Discovery Service URL.

https://dev.crm.dynamics.com/XRMServices/2011/Discovery.svc (North America)


https://dev.crm4.dynamics.com/XRMServices/2011/Discovery.svc (EMEA)


https://dev.crm5.dynamics.com/XRMServices/2011/Discovery.svc (APAC)


The following URLs should be used to access the Organization service(SOAP endpoint):


https://{Organization Name}.api.crm.dynamics.com/XrmServices/2011/Organization.svc (North America)

https://{Organization Name}.api.crm4.dynamics.com/XrmServices/2011/Organization.svc (EMEA)

https://{Organization Name}.api.crm5.dynamics.com/XrmServices/2011/Organization.svc (APAC)

Where {Organization Name} refers to the Organization that you specify in the URL when accessing the Web application. For example, for Contoso.crm.dynamics.com, the {Organization Name} is Contoso.

The following URLs should be used to access the Organization Data service(OData REST endpoint)

https://{Organization Name}.api.crm.dynamics.com/XrmServices/2011/OrganizationData.svc (North America)


https://{Organization Name}.api.crm4.dynamics.com/XrmServices/2011/OrganizationData.svc (EMEA)


https://{Organization Name}.api.crm5.dynamics.com/XrmServices/2011/OrganizationData.svc (APAC)



For CRM On-premises customers:



http://{server}/XRMServices/2011/Discovery.svc for the Discovery service endpoint


http://{server}/{OrgName}/XRMServices/2011/Organization.svc for the Organization Service endpoint (SOAP)


http://{server}/{OrgName}/XRMServices/2011/OrganizationData.svc for the Organization Data Service endpoint (REST)


http://{server}/XRMDeployment/2011/Deployment.svc for the Deployment Service endpoint



2.Install Microsoft Dynamics CRM 2011 SDK.

You can get it here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=eb9c345f-e830-40b8-a5fe-ae7a864c4d76&displaylang=en

3. Run the DeviceRegistration.exe utility to generate a device id and password.

There are two methods to get device id and password.

1.Using DiviceMAnager class available in SDK folder. So that we can generate device id and password.

2.By Commandline.

Once you download SDK from above given link, Inside tools, there will deviceregistration directory. Run it with command line /operation:Register.

Then you will get device id and password. Copy these credentials for further use.

4.Making context class for the organization.svc or discovery service.

C:\Users\aravinda.sheshadri\Desktop\ToolkitGestureDemo\sdk\bin\crmsvcutil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /url:https://<>.crm5.dynamics.com/XRMServices/2011/Organization.svc /o:crm.cs /n:XXXCRM2011 /domain:<>.crm5.dynamics.com /u:<> /p:<>/serviceContextName:XXXcontext /di:116bohmbs2iupfaxp07bp5mhrp /dp:GYfG8LSo%_+3H2H+mpT~w=IG

On run this command, class will be generated containing all entities(context). Here, I have given context name as XXXcontext. So I can access all entities using XXXcontext name.

While running this command, make sure that time in both server and local machine will be same(Time and Timezone).

5. Add this class file to your visual studio 2010 project.



6.Add a reference to the dll’s in there as necessary from SDK folder bin directory:



Microsoft.XRM.sdk

System.ServiceModel

Microsoft.Crm.Sdk.Data.Services;

System.Data.Services.Client;

System.ServiceModel.Description;

Microsoft.Xrm.Sdk;

Microsoft.Xrm.Sdk.Client;

Microsoft.Crm.Services.Utility;

7. How do I access CRM 2011 online wcf service over .net client?



If we are using organization.svc, then we have to use



OrganizationServiceProxy _serviceProxy;

IOrganizationService _service;

ClientCredentials Credentials = new ClientCredentials();

ClientCredentials devivceCredentials = new ClientCredentials();

CRMContext context;



Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;



//This URL needs to be updated to match the servername and Organization for the environment.





Uri OrganizationUri = new Uri("https://{organization name}.crm5.dynamics.com/XRMServices/2011/Organization.svc");



Uri HomeRealmUri = null;



//To get device id and password.



devivceCredentials= DeviceIdManager.LoadDeviceCredentials();





using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, devivceCredentials))

{

serviceProxy.ClientCredentials.UserName.UserName = "username";

serviceProxy.ClientCredentials.UserName.Password = "password";

serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

IOrganizationService service = (IOrganizationService)serviceProxy;

_service = (IOrganizationService)serviceProxy;

context = new CRMContext(_service);

var querry = context.CreateQuery("LeadSet");

var querry1 = from itm in context.LeadSet

select itm;





}


8. Odata(OrganizatationData.svc) is not supported outside the CRM application.

Currently CRM 2011 does not support for Odata(organizationdata.svc) to do any operation from outside the CRM application. So If we make it as web resources, then we can use Odata services.







Proof:

http://mscrmblogger.com/2011/05/12/crm-2011-supported-vs-unsupported/
http://mscrmblogger.com/2011/05/12/crm-2011-supported-vs-unsupported/

http://msdn.microsoft.com/en-us/library/gg334279.aspx

“The REST Endpoint for Web resources provides an alternative interface to work with Microsoft Dynamics CRM data. You can use the REST endpoint to execute HTTP requests by using a service that is based on a Uniform Resource Identifier (URI). The REST endpoint is only available for use by JScript and Silverlight Web resources.”

Thanks


Aravind
aravindbenator@gmail.com



No comments:

Post a Comment