The Microsoft Dynamics CRM 2011 Discovery Service

Roshan Mehta, 04 July 2012

The Microsoft Dynamics CRM 2011 Discovery Service is a web service which lets us determine the organizations that a particular user belongs to. It provides information such as the organisation friendly name, URL endpoints, unique id, version, unique name, URL name, and the current state (whether it is enabled or disabled). In this post, we will take a look at the Discovery Service using a sample application.

The customizations area displays the URLs for each of the available CRM 2011 services under the Developer Resources link.

The Microsoft Dynamics CRM 2011 Discovery Service

 

We will use the URL highlighted in red along with the following code to display the name of the organisations belonging to our CRM deployment.

Uri discoUri = new Uri("http://mycrmserver/XrmServices/2011/Discovery.svc");
NetworkCredential
nc = new NetworkCredential
{
    Domain = "mycrmserver",
    UserName = "roshan",
    Password = "password"
}; 

ClientCredentials cc = new ClientCredentials();
cc.Windows.ClientCredential = nc; 

DiscoveryServiceProxy proxy = new DiscoveryServiceProxy(discoUri, null, cc, null);
proxy.Authenticate(); 

RetrieveOrganizationsRequest request = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse response = (RetrieveOrganizationsResponse)proxy.Execute(request); 

foreach (OrganizationDetail d in response.Details)
{
    Console.WriteLine(d.FriendlyName);
} 

Executing the code above gives the following result:

 The Microsoft Dynamics CRM 2011 Discovery Service

It is important to note that the Discovery Service will only provide information about the organisations the current user has access to. In our code snippet above, I logged in as “roshan” which returned a list of the five organisations I have access to. If I changed the credentials so that we logged in as user “testuser1”, we would get the following result:

 The Microsoft Dynamics CRM 2011 Discovery Service

This user only has access to three of the organisations in our CRM deployment; therefore the call to the Discovery Service under this user’s login will only return three organisations.