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.

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.
LikeLike
Thanks Pratima for pointing out the errors!
I’ll correct it 🙂
LikeLike
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
LikeLike
hi,
If its multi-tenant architecture(Active Directory login) ,
for eg: if there are 2 organizations named ORG1 and ORG2
how the CrmDiscoveryService url will be ?
is it like
http://servername:port/ORG1/mscrmservices/2007/AD/CrmDiscoveryService.asmx
and
http://servername:port/ORG2/mscrmservices/2007/AD/CrmDiscoveryService.asmx
for ORG1and ORG2 respectively
I dont have the multi-tenant architecture here to check it.
Thanks
Prasanth
LikeLike
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;
}
LikeLike
Hi Capri!
Could i know what do you mean by this line
” sets the owner as the one in my web.config file”
Try with CRM Impersonator class !!
Regards,
Nishant Rana
LikeLike
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?
LikeLike
Hi Pramod,
So a custom aspx using a master page throws error.
What error does it throw?
I would give it a try here!
Regards,
Nishant Rana
LikeLike
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
LikeLike
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
LikeLike
can you try this https://nishantrana.me/2009/01/20/error-the-request-failed-with-http-status-401-or-error-4012-unauthorized-while-using-crmservice-in-aspnet-or-windows-application-crm-40/
LikeLike
Yeah I have tried this but no luck…
LikeLike