Sunday, June 6, 2010

How to get the CrmService URL without using the registry

Yes, the Crm Service URL is available in registry but what if the client does not want your custom web page/site to have access the registry? Well here is a simple way to build the SDK URL from the Request object in a web page.

Assumptions:

  • Your page lives in the CrmWeb/ISV folder
    • Which means you custom page will have the same URL as the SDK
  • If you are using an ISV page in a multi org deployment, be sure to use the prependOrgName function or manually append the org name to ISV URL
    • var sUrl = prependOrgName("/ISV/Ascentium/mypage.aspx”)
    • prependOrgName is an unsupported Crm Function.

First the URL Template and the URI Property:

private const string CRM_SDK_URL = "{0}{1}{2}:{3}/mscrmservices/2007/crmservice.asmx";

private Uri _currentUri;

/// <summary>
/// Gets the current URI.
/// </summary>
/// <value>The current URI.</value>
private Uri CurrentUri
{
get
{
if (_currentUri == null)
{
_currentUri = this.Request.Url;
}
return _currentUri;
}
}


Now we use string.Format to create the SDK URL from the URI and the Template:



private CrmService _service;
/// <summary>
/// Gets the current service.
/// </summary>
/// <value>The current service.</value>
private CrmService CurrentService
{
get
{
if(_service == null)
{
_service = new CrmService();
_service.CrmAuthenticationTokenValue = CrmAuthenticationToken.ExtractCrmAuthenticationToken(this.Context, "MY_ORG");
_service.Url = string.Format(CRM_SDK_URL, CurrentUri.Scheme, Uri.SchemeDelimiter, CurrentUri.Host, CurrentUri.Port);
}

return _service;
}
}



And that is it.  Now the SDK Url can be built dynamically from the Request. This method could be adapted to fit other scenarios as well.

No comments: