Question

How to Integrate your application to Microsoft CRM 2011 using CRM 2011 SDK and C#?

Edit: I moved my question to the answer to follow the question and answer format. according to Guido Preite.

Was it helpful?

Solution

Because I'm now in a habit of sharing new things I learn everyday, I'm just gonna show here how I connected to Microsoft CRM 2011 using CRM 2011 SDK and C#. This will help you not to bang your head on the wall like I did a while ago.

First add a reference to your project to the Microsoft.Xrm.Sdk.dll. which you can get from the CRM 2011 sdk(download it here: http://www.microsoft.com/en-us/download/details.aspx?id=24004).

here is the code on how to connect to the CRM Service:

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

    //This is your Organization Service which you can find from the actual CRM UI. go to Settings>Customizations>Developer Resources.
    Uri organizationUri = new Uri("http://crmservername/organizationname/XRMServices/2011/Organization.svc"); 
    Uri homeRealmUri = null;
    ClientCredentials credentials = new ClientCredentials();
    //Instantiate your network credential that will access the CRM Server
    credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password", "domain");

    OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
    //Instantiate IOrganizationService so you can call the CRM service methods.
     IOrganizationService _service = (IOrganizationService)orgProxy

//from this you can now perform CRUD to your CRM. 
//I'm just going to provide some example on how to query your entities in CRM like so:

    QueryExpression query = new QueryExpression() { };

    query.EntityName = "country";
    query.ColumnSet = new ColumnSet("name", "2digitiso", "3digitiso");

    EntityCollection retrieved = _service.RetrieveMultiple(query);

    foreach (var item in retrieved.Entities)
    {
         MessageBox.Show(item["name"].ToString() + " " + item["2digitiso"].ToString() + " " + item["3digitiso"].ToString());
    }

Reference: http://nishantrana.wordpress.com/2010/11/03/sample-code-for-using-iorganizationservice-in-crm-2011/
http://msdn.microsoft.com/en-us/library/gg334708.aspx
http://msdn.microsoft.com/en-us/library/gg328149.aspx
http://www.codeproject.com/Articles/559599/Integrating-your-applications-with-MS-CRM-Online

In Addition if you happen to encounter a exception like this:

CRM Service Exception: Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies

Install : Windows Identity Foundation (http://www.microsoft.com/en-us/download/details.aspx?id=17331)

I hope I helped some of you with your project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top