Export (0) Print
Expand All

RetrieveOrganizations Message (DiscoveryService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Works for all deployment typesWorks online only

Retrieves a list of organizations that a system user belongs to.

The relevant classes are specified in the following table.

TypeClass
RequestRetrieveOrganizationsRequest
ResponseRetrieveOrganizationsResponse

Remarks

The OrganizationDetails array returned in the response contains the name and ID of each organization with the CrmService and MetadataService URL endpoints to access that organization's business data.

Refer to the discovery service Concepts links at the bottom of this page for sample code that demonstrates using the RetrieveOrganizations message with Microsoft Dynamics CRM Online and IFD.

Example

The following code samples show how to retrieve a list of organizations that the system user belongs to in Microsoft Dynamics CRM 4.0 (on-premise).

[C#]
// Provide valid Microsoft Dynamics CRM server information.
string hostname = "localhost";
string port = "80";

// Create the discovery service.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();

// Set the discovery service properties.
discoveryService.UseDefaultCredentials = true;
discoveryService.Url = String.Format("http://{0}:{1}/MSCRMServices/2007/{2}/CrmDiscoveryService.asmx", hostname, port, "AD");

// Retrieve the organization name and endpoint URL from the CrmDiscoveryService Web service.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

OrganizationDetail orgInfo = null;

// Iterate through all the retrieved organizations.
foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
{
                    if (orgDetail.FriendlyName.Equals(orgFriendlyName))
{
orgInfo = orgDetail;
break;
}
}

if (orgInfo == null)
{
throw new Exception("The organization name is invalid.");
}
[Visual Basic .NET]
' Provide valid Microsoft Dynamics CRM server information.
Dim hostname As String = "localhost"
Dim port As String = "80"

' Create the discovery service.
Dim discoveryService As New CrmDiscoveryService()

' Set the discovery service properties.
discoveryService.UseDefaultCredentials = True
discoveryService.Url = String.Format("http://{0}:{1}/MSCRMServices/2007/{2}/CrmDiscoveryService.asmx", hostname, port, "AD")

' Retrieve the organization name and endpoint URL from the CrmDiscoveryService Web service.
Dim orgRequest As New RetrieveOrganizationsRequest()
Dim orgResponse As RetrieveOrganizationsResponse = CType(discoveryService.Execute(orgRequest), RetrieveOrganizationsResponse)

Dim orgInfo As OrganizationDetail = Nothing

' Iterate through all the retrieved organizations
For Each orgDetail As OrganizationDetail In orgResponse.OrganizationDetails
    If orgDetail.FriendlyName.Equals(orgFriendlyName) Then
        orgInfo = orgDetail
        Exit For
    End If
Next orgDetail

If orgInfo Is Nothing Then
    Throw New Exception("The organization name is invalid.")
End If

Example

The following code samples show how to retrieve a list of organizations that the system user belongs to in Microsoft Dynamics CRM Online.

[C#]
// Login information for authentication through the Passport service.
string username = "someone@microsoft.com";
string password = "password";
string partner = "crm.dynamics.com";
string environment = "Production";

// Set the name and TCP port of the server hosting Microsoft Dynamics CRM Online.
string hostname = "dev.crm.dynamics.com";

// Set the friendly name of the target organization.
string orgFriendlyName = "AdventureWorksCycle";

// Create the discovery service.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/{1}/CrmDiscoveryService.asmx", hostname, "Passport");

// Retrieve a policy.
RetrievePolicyRequest policyRequest = new RetrievePolicyRequest();
RetrievePolicyResponse policyResponse = (RetrievePolicyResponse)discoveryService.Execute(policyRequest);

// Retrieve a Passport ticket from the Passport service.
LogonManager lm = new LogonManager();
string passportTicket = lm.Logon(username, password, partner, policyResponse.Policy, environment);

// Retrieve all organizations available to this user.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.PassportTicket = passportTicket;
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

// Iterate through the list of organizations.
foreach (OrganizationDetail org in orgResponse.OrganizationDetails)
{
Console.WriteLine(org.FriendlyName);
}
[Visual Basic .NET]
' Login information for authentication through the Passport service.
Dim username As String = "someone@microsoft.com"
Dim password As String = "password"
Dim partner As String = "crm.dynamics.com"
Dim environment As String = "Production"

' Set the name and TCP port of the server hosting Microsoft Dynamics CRM Online.
Dim hostname As String = "dev.crm.dynamics.com"

' Set the friendly name of the target organization.
string orgFriendlyName = "AdventureWorksCycle"

' Create the discovery service
Dim discoveryService As New CrmDiscoveryService()
discoveryService.Url = String.Format("https://{0}/MSCRMServices/2007/{1}/CrmDiscoveryService.asmx", hostname, "Passport")

' Retrieve a policy.
Dim policyRequest As New RetrievePolicyRequest()
Dim policyResponse As RetrievePolicyResponse = CType(discoveryService.Execute(policyRequest), RetrievePolicyResponse)

' Retrieve a Passport ticket from the Passport service.
Dim lm As New LogonManager()
Dim passportTicket As String = lm.Logon(username, password, partner, policyResponse.Policy, environment)

' Retrieve all organizations available to this user.
Dim orgRequest As New RetrieveOrganizationsRequest()
orgRequest.PassportTicket = passportTicket
Dim orgResponse As RetrieveOrganizationsResponse = CType(discoveryService.Execute(orgRequest), RetrieveOrganizationsResponse)

' Iterate through the list of organizations.
For Each org As OrganizationDetail In orgResponse.OrganizationDetails
    Console.WriteLine(org.FriendlyName)
Next org

See Also

Concepts

Reference


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2015 Microsoft