Follow the steps to retrieve all Organizations using Discovery Service,
Step 1: Create Console Application. Add below References to the project,

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:
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:

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,

Like this:
Like Loading...