Using the CrmDiscoveryService Web Service: IFD
![]() |
[Applies to: Microsoft Dynamics CRM 4.0]
Find the latest SDK documentation: CRM 2015 SDK
For an Internet-facing deployment (IFD) of Microsoft Dynamics CRM, the CrmDiscoveryService Web service can provide a list of CrmService and CrmMetadataService Web service endpoint URLs for each organization that you are a member of. In addition, this Web service is used to obtain a CrmTicket that is required for IFD 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 and a CrmTicket. The sample uses this information to configure the CrmService Web service.
[C#]
// Configure an instance of the CrmDiscoveryService Web service proxy.
CrmDiscoveryService disco = new CrmDiscoveryService();
disco.Url = "https://localhost/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx";
//Retrieve a list of available organizations from the CrmDiscoveryService Web service.
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
// Substitute an appropriate domain, username, and password here.
orgRequest.UserId = domain + "\\" + username;
orgRequest.Password = password;
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)disco.Execute(orgRequest);
//Find the target organization.
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.");
//Retrieve a CrmTicket from the CrmDiscoveryService Web service.
RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
ticketRequest.OrganizationName = orgInfo.OrganizationName;
ticketRequest.UserId = domain + "\\" + username;
ticketRequest.Password = password;
RetrieveCrmTicketResponse ticketResponse =
(RetrieveCrmTicketResponse)disco.Execute(ticketRequest);
[Visual Basic .NET]
' Configure an instance of the CrmDiscoveryService Web service
' proxy.
Dim disco As New CrmDiscoveryService()
disco.Url = _
"https://localhost/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx"
' Retrieve a list of available organizations from the
' CrmDiscoveryService Web service.
Dim orgRequest As New RetrieveOrganizationsRequest()
' Substitute an appropriate domain, username, and password here.
orgRequest.UserId = (domain & "\") + username
orgRequest.Password = password
Dim orgResponse As RetrieveOrganizationsResponse = _
DirectCast(disco.Execute(orgRequest), _
RetrieveOrganizationsResponse)
' Find 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
' Check whether a matching organization was not found.
If orgInfo Is Nothing Then
Throw New Exception("The specified organization was not found.")
End If
' Retrieve a CrmTicket from the CrmDiscoveryService Web service.
Dim ticketRequest As New RetrieveCrmTicketRequest()
ticketRequest.OrganizationName = orgInfo.OrganizationName
ticketRequest.UserId = (domain & "\") + username
ticketRequest.Password = password
Dim ticketResponse As RetrieveCrmTicketResponse = _
CType(disco.Execute(ticketRequest), _
RetrieveCrmTicketResponse)
After the OrganizationDetail and a CrmTicket have been obtained, the organization's CrmService or CrmMetadataService Web services can be configured.
[C#]
//Create the CrmService Web service proxy.
CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
sdktoken.AuthenticationType = 2;
sdktoken.OrganizationName = orgInfo.OrganizationName;
sdktoken.CrmTicket = ticketResponse.CrmTicket;
CrmService = new CrmService();
CrmService.CrmAuthenticationTokenValue = sdktoken;
CrmService.Url = orgInfo.CrmServiceUrl;
[Visual Basic .NET]
' Create the CrmService Web service proxy.
Dim sdktoken As New CrmAuthenticationToken()
sdktoken.AuthenticationType = 2
sdktoken.OrganizationName = orgInfo.OrganizationName
sdktoken.CrmTicket = ticketResponse.CrmTicket
CrmService = New CrmService()
CrmService.CrmAuthenticationTokenValue = sdktoken
CrmService.Url = orgInfo.CrmServiceUrl
You can now call CrmService Web methods.
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. You do have to obtain a CrmTicket before you can configure the CrmAuthenticationToken and CrmService instances, and invoke Web service methods.
See Also
Concepts
.gif)