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…


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

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

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

  1. 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

  2. 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

  3. 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

  4. 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

  5. Hi,
    I am using below code using console for crm 4.0 onpremise to connect crm service :

    NetworkCredential cred = new NetworkCredential();

    cred.Domain = “myorg”;

    cred.UserName=”user”;

    cred.Password = “pwd”;

    CrmAuthenticationToken token = new CrmAuthenticationToken();

    token.AuthenticationType = 0;

    token.OrganizationName = “myorg”;

    CrmService service = new CrmService();

    service.Url = “devcrm/…/CrmService.asmx";

    service.Credentials=new System.Net.NetworkCredential(“Domain”,”UserName”,”Password”);

    Console.WriteLine(“Connected to CRM……”);

    WhoAmIRequest who = new WhoAmIRequest();

    WhoAmIResponse owner = (WhoAmIResponse)service.Execute(who);

    WhoAmIResponse whoResp = (WhoAmIResponse)service.Execute(who);

    Getting error with “The request failed with HTTP status 401: Unauthorized” What could be the solution ?

    Thanks,

    Jharana

    Like

  6. Hi Nishant,
    I am using below code using console to connect to crm 4.0 onpremise :
    NetworkCredential cred = new NetworkCredential();

    cred.Domain = “myorg”;

    cred.UserName=”user”;

    cred.Password = “pwd”;

    CrmAuthenticationToken token = new CrmAuthenticationToken();

    token.AuthenticationType = 0;

    token.OrganizationName = “myorg”;

    CrmService service = new CrmService();

    service.Url = “devcrm/…/CrmService.asmx";

    service.Credentials=new System.Net.NetworkCredential(“Domain”,”UserName”,”Password”);

    Console.WriteLine(“Connected to CRM……”);

    WhoAmIRequest who = new WhoAmIRequest();

    WhoAmIResponse owner = (WhoAmIResponse)service.Execute(who);

    WhoAmIResponse whoResp = (WhoAmIResponse)service.Execute(who);

    Getting error with “The request failed with HTTP status 401: Unauthorized” What could be the solution ?

    Thanks,

    Jharana

    Like

Leave a reply to Jharana Baliyar Singh Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.