Connecting to MS CRM 2011Online

Connecting to MS CRM 2011 Online

In-order to connect to the 2011 Online (even trial version would work), the following steps are necessary:

  1. Download the MS CRM 2011 SDK here.
  2. Install Identity Foundation. Check  c:\Program Files to see if WIF is installed. Else install from here.
  3. Register your development machine , and later your production machine ( your device in other words)  with a LiveDeviceId. This is needed to get connected to the online version.
  4. Prepare your project solution appropriately with the needed references.
  5. Create code to connect to the CRM

Register your device:

In order to register your device , go to \sdk\tools\deviceregistration\ , open the solution in Visual Studio and build it.  After that, inside the bin\Debug  folder, you would find the  ‘deviceregistration.exe’ file.

Open command prompt and navigate to the folder where this deviceregistration.exe exists.

Type in the following:

> deviceregistration.exe /operation:Register

You would get the device id and password, for example:

Device ID: 115rgd3eefxnq5rdb6s3rz9uff

Device Password: ^3!nC!Le?`-oYh0_gy-jqMvo

Save these and keep for your reference later.

Prepare your project solution:

If you need intellisense on your CRM entities,  u need to generate the class file for the same. To do that you could use the ‘CrmSvcUtil.exe’ tool which could be found under sdk\bin

On the command prompt run the following :

CrmSvcUtil.exe /url:https://{organisation}.crm4.dynamics.com/XRMServices/2011/Organization.svc /out:GeneratedCode.cs /username:”windows live id” /password:”live id password” /deviceid:”deviceid” /devicepassword:”device password”

A much handier version:

sdk\bin>CrmSvcUtil.exe /url:https://crmorg1aff5.api.crm.dynamics.com/XRMServices/2011/Organization.svc /out:GeneratedCode.cs /username:”mailuser@mail.com” /password:”password” /deviceid:”115rgd3eeeenq5rdb6s3qz9uff” /devicepassword:”^3!NJHU?`-oYh0_gy+jqMvo”

For the complete coverage on the Organisation Uri , refer this.

On running this you would see the ‘GeneratedCode.cs’ file inside the bin directory itself. You could specify a different path altogether. Include this class file into your project.

Now let’s start a test page which connects to CRM online, and then get a particular lead.

Include all the needed dlls from sdk\bin to your project.

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

Now include ‘deviceidmanager.cs’ class which is shipped along with the SDK.  For this to work have the reference to  ‘System.Security’

Now the piece of code which ‘CONNECTS’ to CRM , could be as simple a function which could be used anywhere:

public IOrganizationService getCRMOnlineService()
    {
        Uri OrganizationUri = new Uri(ConfigurationManager.AppSettings["OrganizationUri"].ToString());
        ClientCredentials DeviceCredentials = Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();
        ClientCredentials Credentials = new ClientCredentials();

        Credentials.UserName.UserName = ConfigurationManager.AppSettings["crmUserName"].ToString();
        Credentials.UserName.Password = ConfigurationManager.AppSettings["crmPassword"].ToString();

        OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, null, Credentials, DeviceCredentials);
        serviceProxy.EnableProxyTypes();

        IOrganizationService _service = (IOrganizationService)serviceProxy;
        return _service;
    }

The config file should contain all the needed data as below:

<add key="OrganizationUri" value="https://{orgname}/XRMServices/2011/Organization.svc"/>
<add key="crmUserName" value="windowsliveid@mail.com"/>
<add key="crmPassword" value="password"/>

The code to get a lead via fetchXML:

public void getLead(Guid guidLeadId, IOrganizationService _service)
    {
        string fetch2 = "<fetch mapping='logical' returntotalrecordcount='true'>" +
                 "<entity name='lead'>" +
                 "<all-attributes/>" +
                 "<filter type='and'>" +
                 "<condition attribute = 'leadid' operator='eq' value='" + guidLeadId.ToString() + "'/>" +
                  "</filter>" +
                  "</entity>" +
              "</fetch>";
        EntityCollection result = _service.RetrieveMultiple(new FetchExpression(fetch2));
        if (result.TotalRecordCount > 0)
        {
            Lead objLead = (Lead)result[0];
            Response.Write(objLead.FirstName.ToString() + " --- " + objLead.OwnerId.Id.ToString() + "----" + objLead.OwnerId.Name);
        }
        else
        {
            Response.Write("No lead with the given Lead Id<br/>");
        }
    }

Same way you could have all the CRUD operations done.

Hope this helps 🙂

Advertisements

About mytechlifedays

Its been a wonderful learning period over the last 6 years in the IT industry , getting exposed to whole lot of technologies and ideas. The hurdles and the crisis that came along have been wonderful experience ... And now its time to pen them down so that let some others execute faster and easily with these information .....
This entry was posted in MS CRM and tagged , , , . Bookmark the permalink.

3 Responses to Connecting to MS CRM 2011Online

  1. Pingback: Configuration binding extension ‘system.serviceModel/bindings/netTcpRelayBinding’ could not be found. | My Tech Days

  2. Hey There. I found your blog using msn. This
    is a very well written article. I will make sure to bookmark it and return to read more of your useful
    info. Thanks for the post. I’ll definitely comeback.

Leave a Reply ! It would be always appreciated ! :)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s