This documentation is archived and is not being maintained.

Using the CrmDiscoveryService Web Service: On-Premise

Forefront
banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

For an Active Directory installation of Microsoft Dynamics CRM, the CrmDiscoveryService Web service can provide a list of Web service endpoint URLs for each organization that you are a member of. That information is used to configure the CrmService and CrmMetadataService Web service proxies and call Web service methods to access an organization's data.

The following sample code shows you how to use the Web service to obtain information for a fictitious AdventureWorksCycle organization hosted on a Microsoft Dynamics CRM server named localhost.

[C#]

// Create and configure the CrmDiscoveryService Web service proxy.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.UseDefaultCredentials = true;
discoveryService.Url = "http://localhost/MSCRMServices/2007/AD/CrmDiscoveryService.asmx";

// Retrieve the list of organizations that the logged on user belongs to.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse =
    (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

// Locate the target organization in the list. 
OrganizationDetail orgInfo = null;
foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
{
    if (orgDetail.OrganizationName.Equals("AdventureWorksCycle"))
    {
        orgInfo = orgDetail;
        break;
    }
}

// Check whether a matching organization was not found. 
if (orgInfo == null)
    throw new Exception("The specified organization was not found.");

[Visual Basic .NET]

' Create and configure the CrmDiscoveryService Web service 
' proxy.
Dim discoveryService As New CrmDiscoveryService()
discoveryService.UseDefaultCredentials = True
discoveryService.Url = _
"http://localhost/MSCRMServices/2007/AD/CrmDiscoveryService.asmx"

' Retrieve the list of organizations that the logged on user 
' belongs to.
Dim orgRequest As New RetrieveOrganizationsRequest()
Dim orgResponse as RetrieveOrganizationsResponse = _
   CType(discoveryService.Execute(orgRequest), _
   RetrieveOrganizationsResponse)

' Locate the target organization in the list. 
Dim orgInfo As OrganizationDetail = Nothing
For Each orgDetail As OrganizationDetail In _
   orgResponse.OrganizationDetails
    If orgDetail.OrganizationName = "AdventureWorksCycle"
       orgInfo = orgDetail
       Exit For
    End If
Next

' Check whether a matching organization was not found. 
If orgInfo Is Nothing
    Throw New Exception("The specified organization was not found.")
End If

After the OrganizationDetail for the target organization has been obtained, the CrmService and CrmMetadataService Web services can be accessed through the provided URLs in OrganizationDetail to obtain the organization's business data.

[C#]

CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = orgInfo.OrganizationName;

CrmService crmService = new CrmService();
crmService.Url = orgInfo.CrmServiceUrl;
crmService.CrmAuthenticationTokenValue = token;
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;

[Visual Basic .NET]

Dim token As New CrmAuthenticationToken()
token.AuthenticationType = 0
token.OrganizationName = orgInfo.OrganizationName

Dim crmService As New CrmService()
crmService.Url = orgInfo.CrmServiceUrl
crmService.CrmAuthenticationTokenValue = token
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials

If you know the CrmService and CrmMetadataService Web service URLs for the target organization and the name of the organization, you do not have to use the CrmDiscoveryService Web service. You can configure the CrmAuthenticationToken and CrmService instances and invoke Web methods.

See Also

Concepts


© 2010 Microsoft Corporation. All rights reserved.


Show: