Understanding and Using CrmDiscoveryService in CRM 4.0

This is a new web service introduced in CRM 4.0.

Using it we can get organization information as well as organization’s CrmService and MetadataService web service’s url end points.

The web service has three different end point based on authentication mode

Active Directory

http://servername:port/mscrmservices/2007/AD/CrmDiscoveryService.asmx

IFD –

http://servername:port/mscrmservices/2007/IFD/CrmDiscoveryService.asmx

Online –

https://dev.crm.dynamics.com/mscrmservices/2007/passport/CrmDiscoveryService.asmx

In case of IFD and Online, we neet to obtain CrmTicket information that is required.

In case of Online we also need policy information and Passport ticket.

These information provided by CrmDiscoveryService is used for configuring CrmAuthenticationToken and CrmService instances.

For on-premise

If we already know organization name and webservice Url end points that it is not necessary for us to use CrmDiscoveryService.

This is how we can use the CrmDiscoveryService for configuring our Crm Service

CrmDiscoveryService myDiscoveryService = new CrmDiscoveryService();

myDiscoveryService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myDiscoveryService.Url = “validurl.asmx”;

// Retrieving the list of organization that user belongs

RetrieveOrganizationsRequest myRetrieveOrgRequest = new RetrieveOrganizationsRequest();

RetrieveOrganizationsResponse myRetrieveOrgResponse = (RetrieveOrganizationsResponse)myDiscoveryService.Execute(myRetrieveOrgRequest);

// Get organization information

OrganizationDetail myOrgDetail = null;

foreach (OrganizationDetail orgDetail in myRetrieveOrgResponse.OrganizationDetails)

{

if (orgDetail.Equals(“MyOrgName”))

{

myOrgDetail = orgDetail;

break;

} }

// Setting Crm Authentication Token

CrmAuthenticationToken myCrmAuthToken = new CrmAuthenticationToken();

// 0- AD , 1- IFD , 2 – Windows Live

myCrmAuthToken.AuthenticationType = 0;

myCrmAuthToken.OrganizationName = myOrgDetail.OrganizationName;

// Finally setting our CrmService

CrmService myCrmService = new CrmService();

myCrmService.CrmAuthenticationTokenValue = myCrmAuthToken;

myCrmService.Url = myOrgDetail.CrmServiceUrl;

In case of IFD, we need CrmTicket for configuring CrmAuthenticationToken and CrmService.

CrmDiscoveryService myDiscoveryService = new CrmDiscoveryService();

myDiscoveryService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myDiscoveryService.Url = “validurl.asmx”;

// Retrieving the list of organization that user belongs

RetrieveOrganizationsRequest myRetrieveOrgRequest = new RetrieveOrganizationsRequest();

myRetrieveOrgRequest.UserId = @”domain\username”;

myRetrieveOrgRequest.Password = “password”;

RetrieveOrganizationsResponse myRetrieveOrgResponse = (RetrieveOrganizationsResponse)myDiscoveryService.Execute(myRetrieveOrgRequest);

// Get organization information

OrganizationDetail myOrgDetail = null;

foreach (OrganizationDetail orgDetail in myRetrieveOrgResponse.OrganizationDetails)

{

if (orgDetail.Equals(“MyOrgName”))

{

myOrgDetail = orgDetail;

break;

} }

RetrieveCrmTicketRequest myTickectRequest = new RetrieveCrmTicketRequest();

myTickectRequest.OrganizationName = myOrgDetail.OrganizationName;

myTickectRequest.UserId=@”domain\username”;

myTickectRequest.Password = “password”;

RetrieveCrmTicketResponse myTicketResponse = (RetrieveCrmTicketResponse)myDiscoveryService.Execute(myTickectRequest);

// Setting Crm Authentication Token

CrmAuthenticationToken myCrmAuthToken = new CrmAuthenticationToken();

myCrmAuthToken.CrmTicket = myTicketResponse.CrmTicket;

// 0- AD , 1- IFD , 2 – Windows Live

myCrmAuthToken.AuthenticationType = 1

myCrmAuthToken.OrganizationName = myOrgDetail.OrganizationName;

// Finally setting our CrmService

CrmService myCrmService = new CrmService();

myCrmService.CrmAuthenticationTokenValue = myCrmAuthToken;

myCrmService.Url = myOrgDetail.CrmServiceUrl;

In case of Windows Online deployment

We need policy information, passport ticket and CrmTicket for configuring configuring CrmAuthenticationToken and CrmService.

LogonManager – This class is used to authenticate a user with the Windows Live ID service.

To access this class we need to add a reference to following idcrlwrapper.dll

The project can be found at sdk\server\helpers\cs\idcrlwrapper

CrmDiscoveryService myDiscoveryService = new CrmDiscoveryService();

myDiscoveryService.Credentials = System.Net.CredentialCache.DefaultCredentials;

myDiscoveryService.Url = https://dev.crm.dynamics.com/mscrmservices/2007/passport/CrmDiscoveryService.asmx“;

// Retireve Policy Information

RetrievePolicyRequest myPolicyRequest = new RetrievePolicyRequest();

RetrievePolicyResponse myPolicyResponse = (RetrievePolicyResponse)myDiscoveryService.Execute(myPolicyRequest);

LogonManager myLogonManager = new LogonManager();

string myPassportTicket = myLogonManager.Logon(“username”, “password”, “partner”, myPolicyResponse.Policy, “environment”);

myLogonManager.Dispose();

// Retrieving the list of organization that user belongs

RetrieveOrganizationsRequest myRetrieveOrgRequestLive = new RetrieveOrganizationsRequest();

myRetrieveOrgRequestLive.PassportTicket = myPassportTicket;

RetrieveOrganizationsResponse myRetrieveOrgResponseLive = (RetrieveOrganizationsResponse)myDiscoveryService.Execute(myRetrieveOrgRequestLive);

// Get organization information

OrganizationDetail myOrgDetailLive = null;

foreach (OrganizationDetail orgDetail in myRetrieveOrgResponseLive.OrganizationDetails)

{

if (orgDetail.Equals(“MyOrgName”))

{

myOrgDetailLive = orgDetail;

break;

}

}

RetrieveCrmTicketRequest myTickectRequest = new RetrieveCrmTicketRequest();

myTickectRequest.OrganizationName = myOrgDetail.OrganizationName;

myTickectRequest.PassportTicket = myPassportTicket;

RetrieveCrmTicketResponse myTicketResponse = (RetrieveCrmTicketResponse)myDiscoveryService.Execute(myTickectRequest);

// Setting Crm Authentication Token

CrmAuthenticationToken myCrmAuthToken = new CrmAuthenticationToken();

myCrmAuthToken.CrmTicket = myTicketResponse.CrmTicket;

// 0- AD , 1- IFD , 2 – Windows Live

myCrmAuthToken.AuthenticationType = 2;

myCrmAuthToken.OrganizationName = myOrgDetail.OrganizationName;

// Finally setting our CrmService

CrmService myCrmService = new CrmService();

myCrmService.CrmAuthenticationTokenValue = myCrmAuthToken;

myCrmService.Url = myOrgDetail.CrmServiceUrl;

Bye…


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

8 thoughts on “Understanding and Using CrmDiscoveryService in CRM 4.0”

  1. hi nishant,
    I m trying to access a custom aspx page from CRM with relative path in sitemap.
    when i try above scenerio it works fine.
    but when the page has a master page it fails to load.
    Can you suggest something?

    Like

  2. Hi Nishant,

    I am working in an iFD environment for my client.
    I have folllowed the instructionsa in your code above but somehow the crm service I cretae always sets the owner as the one in my web.config file , although I am using “disco.Credentials = System.Net.CredentialCache.DefaultCredentials;” Pls find my code below. :

    //Return IFD Crm service
    public CrmService GetCrmService()
    {
    try
    {
    string organization = ConfigurationManager.AppSettings[“OrgName”].ToString();
    string server = ConfigurationManager.AppSettings[“ServerName”].ToString();
    string domain = ConfigurationManager.AppSettings[“CRMDomain”].ToString();
    string username = ConfigurationManager.AppSettings[“CRMUserName”].ToString();
    string password = ConfigurationManager.AppSettings[“CRMPassword”].ToString();

    // Configure an instance of the CrmDiscoveryService Web service proxy.
    CrmDiscoveryService disco = new CrmDiscoveryService();
    disco.Url = “http://” + server + “/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx”;
    disco.Credentials = System.Net.CredentialCache.DefaultCredentials;

    //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 = 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(organization))
    {
    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 = username;
    ticketRequest.Password = password;
    RetrieveCrmTicketResponse ticketResponse =
    (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);

    //Create the CrmService Web service proxy.
    CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
    sdktoken.AuthenticationType = 2;
    sdktoken.OrganizationName = orgInfo.OrganizationName;
    sdktoken.CrmTicket = ticketResponse.CrmTicket;

    CrmService service = new CrmService();
    service.CrmAuthenticationTokenValue = sdktoken;
    service.Url = orgInfo.CrmServiceUrl;
    }
    catch (SoapException ex)
    {
    ErrHelper.ErrorWriter(ex, “Error Creating CRM service”, “error in crm service craete”, FileName);
    return null;
    }
    }
    return service;
    }

    Like

  3. Hi

    1. In CRM Dynamics 4.0 ,I have created my entity “Property” and want to link it with Business Unit. On the form I want to get the list of BU for the user to select the appropriate BU related to the Property. The relationship is 1:1 How to do it ?

    2. I have a custom entity ,and i want to add buttons etc to it. The ISV.config export does not give any detail about the cutom entity. How to do that ?

    Regards
    SN

    Like

  4. Hello Nishant,
    Thank you very much for the Information. It really provided a great starting point of understanding the discovery service. However, I would like to bring it to your notice small errors that occured in your code that might give an 401:Unauthorized error. One is the Authentication Type code. According to SDK, the correct codes are:
    0 – On premise
    1 – Crm Online
    2 – IFD
    Another one is that the Url for Crm Discovery Service for IFD, the correct URL is http://servername/mscrmservices/2007/SPLA/CrmDiscoveryService.asmx. Thanks for the Information.

    Pratima.

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s