Get all CRM Organizations using Discovery Service in C# and SDK

Follow the steps to retrieve all Organizations using Discovery Service,

Step 1: Create Console Application. Add below References to the project,

Discovery Service References

Step 2: Include the below namespaces,

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;

Step 3: Use the below Code to get all organizations using Discovery Service,

OnPremises

OnPremises:

public static List<string> GetOrganizations(string DiscoverServiceURL, string UserName, string Password, string Domain)
{
ClientCredentials credentials = new ClientCredentials();

credentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password, Domain);
using (var discoveryProxy = new DiscoveryServiceProxy(new Uri(DiscoverServiceURL), null, credentials, null))
{
discoveryProxy.Authenticate();

// Get all Organizations using Discovery Service

RetrieveOrganizationsRequest retrieveOrganizationsRequest =
new RetrieveOrganizationsRequest()
{
AccessType = EndpointAccessType.Default,
Release = OrganizationRelease.Current
};

RetrieveOrganizationsResponse retrieveOrganizationsResponse =
(RetrieveOrganizationsResponse)discoveryProxy.Execute(retrieveOrganizationsRequest);

if (retrieveOrganizationsResponse.Details.Count >0)
{
var orgs = new List<String>();

foreach (OrganizationDetail orgInfo in retrieveOrganizationsResponse.Details)
orgs.Add(orgInfo.FriendlyName);

return orgs;
}
else
return null;
}
}

Usage:

GetOrganizations("https://XXXXXX/XRMServices/2011/Discovery.svc", "arunpotti@XXXXX.onmicrosoft.com", "P@ssw0rd!", "D0m@in");

Online:

OnPremises

public static List<string> GetOrganizations(string DiscoverServiceURL, string UserName, string Password)
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;

using (var discoveryProxy = new DiscoveryServiceProxy(new Uri(DiscoverServiceURL), null, credentials, null))
{
discoveryProxy.Authenticate();

// Get all Organizations using Discovery Service

RetrieveOrganizationsRequest retrieveOrganizationsRequest = new RetrieveOrganizationsRequest()
{
AccessType = EndpointAccessType.Default,
Release = OrganizationRelease.Current
};

RetrieveOrganizationsResponse retrieveOrganizationsResponse =
(RetrieveOrganizationsResponse)discoveryProxy.Execute(retrieveOrganizationsRequest);

if (retrieveOrganizationsResponse.Details.Count > 0)
{
var orgs = new List<String>();
foreach (OrganizationDetail orgInfo in retrieveOrganizationsResponse.Details)
orgs.Add(orgInfo.FriendlyName);

return orgs;
}
else
return null;
}
}

Usage:

GetOrganizations("https://disco.crm.dynamics.com/XRMServices/2011/Discovery.svc", "arunpotti@XXX.onmicrosoft.com", "P@ssw0rd!");

Step 4: Final Code is here,

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;

namespace GetOrganizations
{
class Program
{
static void Main(string[] args)
{
try
{
string OrgNames = string.Empty;

//Connecting to Online

List<string> lstOrgs = GetOrganizations("https://disco.crm.dynamics.com/XRMServices/2011/Discovery.svc", "arpotti@XXX.onmicrosoft.com", " P@ssw0rd!");

if (lstOrgs.Count > 0)
{
foreach (var Org in lstOrgs)
{
OrgNames = OrgNames + Org +"\n";
}

Console.Write("Organizations\n--------------\n"+OrgNames);                    Console.ReadKey();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

public static List<string> GetOrganizations(string DiscoverServiceURL, string UserName, string Password, string Domain)
{
ClientCredentials credentials = new ClientCredentials();            credentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password, Domain);

using (var discoveryProxy = new DiscoveryServiceProxy(new Uri(DiscoverServiceURL), null, credentials, null))
{
discoveryProxy.Authenticate();

// Get all Organizations using Discovery Service

RetrieveOrganizationsRequest retrieveOrganizationsRequest = new RetrieveOrganizationsRequest()
{
AccessType = EndpointAccessType.Default,
Release = OrganizationRelease.Current
};

RetrieveOrganizationsResponse retrieveOrganizationsResponse =
(RetrieveOrganizationsResponse)discoveryProxy.Execute(retrieveOrganizationsRequest);

if (retrieveOrganizationsResponse.Details.Count >0)
{
var orgs = new List<String>();
foreach (OrganizationDetail orgInfo in retrieveOrganizationsResponse.Details)
orgs.Add(orgInfo.FriendlyName);
return orgs;
}
else
return null;
}
}

public static List<string> GetOrganizations(string DiscoverServiceURL, string UserName, string Password)
{
ClientCredentials credentials = new ClientCredentials();
credentials.UserName.UserName = UserName;
credentials.UserName.Password = Password;

using (var discoveryProxy = new DiscoveryServiceProxy(new Uri(DiscoverServiceURL), null, credentials, null))
{
discoveryProxy.Authenticate();

// Get all Organizations using Discovery Service

RetrieveOrganizationsRequest retrieveOrganizationsRequest =
new RetrieveOrganizationsRequest()
{
AccessType = EndpointAccessType.Default,
Release = OrganizationRelease.Current
};

RetrieveOrganizationsResponse retrieveOrganizationsResponse =
(RetrieveOrganizationsResponse)discoveryProxy.Execute(retrieveOrganizationsRequest);

if (retrieveOrganizationsResponse.Details.Count > 0)
{
var orgs = new List<String>();
foreach (OrganizationDetail orgInfo in retrieveOrganizationsResponse.Details)
orgs.Add(orgInfo.FriendlyName);

return orgs;
}
else
return null;
}
}
}
}

Step 5: Final Output is below,

Discovery Service Output

Advertisements

5 thoughts on “Get all CRM Organizations using Discovery Service in C# and SDK

  1. Hi Arun,

    Nice Post.

    I am facing this error while getting List of all Organizations on below line

    using (var discoveryProxy = new DiscoveryServiceProxy(new Uri(DiscoverServiceURL), null, credentials, null))

    Could not load file or assembly ‘Microsoft.Crm.Site.Services

    The service ‘%2fXRMServices%2f2011%2fDiscovery.svc’ cannot be activated due to an exception during compilation. The exception message is: Could not load file or assembly ‘Microsoft.Crm.Site.Services%2c Version%3d7.0.0.0%2c Culture%3dneutral%2c PublicKeyToken%3d31bf3856ad364e35′ or one of its dependencies. The system cannot find the file specified..&RequestUri=%2fXRMServices%2f2011%2fDiscovery.svc%3fwsdl&user_lcid=1025’.

    Any refrence idea?

    Like

    • Hi Abhishek,

      Are you passing the correct Discovery Service URL? Check one more time, Open CRM and Goto Settings –> Customizations –> Developer Resources –> Discovery Service URL.

      Let me know if you face any issues.

      Like

  2. hi,,Arun
    i am getting soap securirity error in on-premise crm
    SOAP security negotiation with
    ‘http://xxx/XRMServices/2011/Discovery.svc’ for target ‘http://xxx/XRMServices/2011/Discovery.svc’ failed. See inner exception for more details.
    Any Solution?

    Like

Leave a Reply

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