CRM 2011 get List of all Organizations and get url from organization details collection

Posted on Posted in CRM 2011 SDK, MS CRM 2011, MS Dynamics, MS Dynamics CRM

CRM 2011 get List of all Organizations and get url from organization details collection
How to get URL from list of organisations when getting data back from OrganizationDetailCollection

Method to get list of OrganizationDetailCollection
public static OrganizationDetailCollection GetOrganizations()
string url = CrmServerConnection.GetMSCRMServerUrl();

if (string.IsNullOrEmpty(url))
url = ConfigurationManager.AppSettings[“MSCRM”];

System.Diagnostics.Debug.WriteLine(“Crm11Services: GetOrganizations() Get MSCRM Url: ” + url);

IServiceConfiguration dinfo =
ServiceConfigurationFactory.CreateConfiguration( GetDisSerUri (url));

var creds = new ClientCredentials();
creds.Windows.ClientCredential = (System.Net.NetworkCredential)System.Net.CredentialCache.DefaultCredentials;

DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(dinfo, creds);

RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse = dsp.Execute(orgRequest) as RetrieveOrganizationsResponse;

var readas = orgResponse.Details;
return orgResponse.Details;

Below is the method to get Uri

private static Uri GetDisSerUri(string serv)
string dis = @”/XRMServices/2011/Discovery.svc”;
return new Uri(string.Format(“{0}{1}”, serv, dis));

In above method we can get list of all organizations
Once we loop through these organizations we can get uri and all other data in code below
foreach (var od in allOrgs)
CrmOrganisation org = new CrmOrganisation();
org.OrgId = od.OrganizationId;
org.OrgName = od.FriendlyName;
org.FriendlyName = od.FriendlyName;

//get exact url
foreach (var endpoint in od.Endpoints)
if (endpoint.Key == EndpointType.WebApplication)
org.Url = endpoint.Value.ToString();