09/12/2012

Connecting to CRM Online with Visual Studio 2012 and Windows 8


You have created your trial CRM online org and you want to write a code to show the org name of the newly created CRM org. Here is a brief overview on how to do it:

Turning on Windows Identity Foundation 3.5


From the control panel, click on Programs and Feature, click “Turn windows features on or off” and enable Windows Identity Foundation 3.5

 Note: Skipping this step will result the following error message:
"Could not load file or assembly 'Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=xxxx or one of its dependencies. The system cannot find the file specified."

Create a new Console application in visual studio 2012


 

Right click on your newly created project and choose add existing item and choose “deviceidmanager” class from the CRM SDK helper files

“{Your downloaded SDK location}\samplecode\cs\helpercode

Your project may look like this at this stage

 

Right click on the references folder and add the following assemblies

  • System.ServiceModel
  • System.Security (otherwise you will get at least this error: the name "ProtectedData" does not exist in the current context) 
  • Microsoft.xrm.sdk (From CRM SDK)

Add the following using statements:

//Namespcae of deviceidmanager class you have added

using Microsoft.Crm.Services.Utility;

//You have referenced System.ServiceModel for ClientCredentials

using System.ServiceModel.Description;

//You have referenced microsoft.xrm.sdk for DiscoveryServiceProxy

using Microsoft.Xrm.Sdk.Client;

//For request and response for retrieving your CRM online org

using Microsoft.Xrm.Sdk.Discovery;

The following code will retrieve the org name from your CRM online version

 

 
 
 
class Program
    {
        static void Main(string[] args)
        {
            string discoveryURL = "https://disco.crm5.dynamics.com/XRMServices/2011/Discovery.svc"; 
            ClientCredentials deviceCredentials = DeviceIdManager.LoadOrRegisterDevice();
            ClientCredentials credentials = new ClientCredentials();
            //When creating a CRM online trial version, through Microsoft Online services you will setup a username and password
            credentials.UserName.UserName ="YourOnlineUserName.onmicrosoft.com";
            credentials.UserName.Password = "YourPassword";

            DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(
                new Uri(discoveryURL),
                null,
                credentials,
                deviceCredentials
                );

            // try to authenticate
            dsp.Authenticate();

            //create a request and response to retrive your Org
            RetrieveOrganizationsRequest req = new RetrieveOrganizationsRequest();
            RetrieveOrganizationsResponse resp = (RetrieveOrganizationsResponse)dsp.Execute(req);

            foreach (var o in resp.Details)
            {
                //Show the friendly name of your CRM online org
                Console.WriteLine(o.FriendlyName);
            }

            Console.ReadKey(true);
        }
    }

1 comment:

  1. hi,

    I run the code on vs2012 for windows store app, Its giving exception :

    Could not load file or assembly 'System.ServiceModel.Channels, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

    ReplyDelete