Using the CrmDiscoveryService Web Service: CRM Online

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

For Microsoft Dynamics CRM Online, the CrmDiscoveryService Web service can provide a list of Web service endpoint URLs for each organization that you are a member of. In addition, this Web service is used to obtain a policy and CrmTicket that are required for Windows Live authentication. This information is then used to configure the CrmService and CrmMetadataService Web service instances.

The following sample code shows you how to use the CrmDiscoveryService Web service to obtain organization information for a fictitious AdventureWorksCycle organization, a policy, and a CrmTicket. The sample uses this information to configure the CrmService Web service.

[C#]

// Create and configure the CrmDiscoveryService Web service.
CrmDiscoveryService discoveryService = new CrmDiscoveryService();
discoveryService.Url = 
    "https://dev.crm.dynamics.com/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx";

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

// Retrieve a ticket from the Windows Live (Passport) service.
LogonManager lm = new LogonManager();
string passportTicket = lm.Logon(_username, _password, _partner,
    policyResponse.Policy, _environment);
// Dispose of the LogonManager object to avoid a FileNotOpen exception.
lm.Dispose();

// Retrieve a list of organizations that the logged on user is a member of.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.PassportTicket = passportTicket;
RetrieveOrganizationsResponse orgResponse =
    (RetrieveOrganizationsResponse)discoveryService.Execute(orgRequest);

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

if (orgInfo == null)
    throw new Exception("The specified organization was not found.");

// Retrieve a CrmTicket from the Web service.
RetrieveCrmTicketRequest crmTicketRequest = new RetrieveCrmTicketRequest();
crmTicketRequest.OrganizationName = orgInfo.OrganizationName;
crmTicketRequest.PassportTicket = passportTicket;

RetrieveCrmTicketResponse crmTicketResponse =
    (RetrieveCrmTicketResponse)discoveryService.Execute(crmTicketRequest); 

[Visual Basic .NET]

' Create and configure the CrmDiscoveryService Web service. 
Dim discoveryService As New CrmDiscoveryService() 
discoveryService.Url = _
"https://dev.crm.dynamics.com/MSCRMServices/2007/Passport/CrmDiscoveryService.asmx" 

' Retrieve a policy from the Web service. 
Dim policyRequest As New RetrievePolicyRequest() 
Dim policyResponse As RetrievePolicyResponse = _
   DirectCast(discoveryService.Execute(policyRequest), _
   RetrievePolicyResponse) 

' Retrieve a ticket from the Windows Live (formerly Passport) service. 
Dim lm As New LogonManager() 
Dim passportTicket As String = lm.Logon(_username, _password, _partner, policyResponse.Policy, _environment) 
' Dispose of the LogonManager object to avoid a FileNotOpen exception. 
lm.Dispose() 

' Retrieve a list of organizations that the logged on user is a member of. 
Dim orgRequest As New RetrieveOrganizationsRequest() 
orgRequest.PassportTicket = passportTicket 
Dim orgResponse As RetrieveOrganizationsResponse = _
   CType(discoveryService.Execute(orgRequest), _
   RetrieveOrganizationsResponse) 

' Locate the target organization. 
Dim orgInfo As OrganizationDetail = Nothing 
For Each orgDetail As OrganizationDetail In orgResponse.OrganizationDetails 
    If orgDetail.OrganizationName.Equals("AdventureWorksCycle") Then 
        orgInfo = orgDetail 
        Exit For 
    End If 
Next 

If orgInfo Is Nothing Then 
    Throw New Exception("The specified organization was not found.") 
End If 

' Retrieve a CrmTicket from the Web service. 
Dim crmTicketRequest As New RetrieveCrmTicketRequest() 
crmTicketRequest.OrganizationName = orgInfo.OrganizationName 
crmTicketRequest.PassportTicket = passportTicket 

Dim crmTicketResponse As RetrieveCrmTicketResponse = _
   CType(discoveryService.Execute(crmTicketRequest), _
   RetrieveCrmTicketResponse)

After the OrganizationDetail and a CrmTicket have been obtained, the organization's CrmService and CrmMetadataService Web services can be configured.

[C#]

// Create and configure an instance of the CrmService Web service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = AuthenticationType.Passport;
token.CrmTicket = crmTicketResponse.CrmTicket;
token.OrganizationName = crmTicketResponse.OrganizationDetail.OrganizationName;

CrmService crmService = new CrmService();
crmService.Url = crmTicketResponse.OrganizationDetail.CrmServiceUrl;
crmService.CrmAuthenticationTokenValue = token; 

[Visual Basic .NET]
' Create and configure an instance of the CrmService Web 
' service. 
Dim token As New CrmAuthenticationToken() 
token.AuthenticationType = AuthenticationType.Passport 
token.CrmTicket = crmTicketResponse.CrmTicket 
token.OrganizationName = _
   crmTicketResponse.OrganizationDetail.OrganizationName 

Dim crmService As New CrmService() 
crmService.Url = _
   crmTicketResponse.OrganizationDetail.CrmServiceUrl 
crmService.CrmAuthenticationTokenValue = token

If you know the Web service URLs for the organization that you are a member of and the name of the organization, you do not have to use the CrmDiscoveryService Web service to retrieve the list of organizations. However, you do have to obtain a policy, Passport ticket, and CrmTicket before you can configure the CrmAuthenticationToken and CrmService instances and invoke Web service methods.

See Also

Concepts

© 2010 Microsoft Corporation. All rights reserved.