locked
How does "Xrm.Page.context.getServerUrl" work? RRS feed

  • Question

  • Hello,

    We have just installed a new dev server for CRM 2011.

    In my javascript I use Xrm.Page.context.getServerUrl to get the URL so I can connect to the oData endpoint.

    However in my new environemnt the URL returned is not the URL which resolves to CRM.

    For example, to browse to CRM I use

    http://myservername.mycompany.com/myorg/

    However Xrm.Page.context.getServerUrl() returns

    http://myservername/myorg/

    Does anyone know where the getServerUrl get's the server URL from?

    regards,

    JdZ

    Thursday, February 2, 2012 4:56 PM

Answers

  • getServerUrl will provide the url that is configured in the Web Address tab of the deployment properties in the Crm Deployment Manager tool
    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk
    Thursday, February 2, 2012 5:05 PM
    Moderator
  • Instead of writing a wrapper, here is what I do for all my JScript & Silverlight calls:

    var _url = "/" + Xrm.Page.context.getUniqueOrgName() + "/XRMServices/2011/OrganizationData.svc"

    It's worked like a charm and since I'm using a relative path I don't have to deal with the cross-domain pieces. 

    Jeremy

    Friday, February 3, 2012 12:49 AM

All replies

  • getServerUrl will provide the url that is configured in the Web Address tab of the deployment properties in the Crm Deployment Manager tool
    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk
    Thursday, February 2, 2012 5:05 PM
    Moderator
  • If you need the URL to match the URL that is actually being accessed, you'd have to wrap the getServerUrl function in your own function and do some regex replacement on the URL's host for the current window's host (window.location.host). You'll probably also want to catch the protocol being accessed as well (http vs https).
    Thursday, February 2, 2012 7:24 PM
  • Hello,

    You can have a look at the global JavaScript variables that CRM generates by viewing the HTML source of any CRM page, like main.aspx

    You will see a lot of JavaScript variables which you can access on any CRM form.

    Have a look at these variables, of interest to you will be WEB_SERVER_HOST, ORG_UNIQUE_NAME, WEB_SERVER_PORT and other variables which record the authentication type like IS_SPLA (IFD) and IS_ONPREMISE.

    Using these you could construct your own getServerUrl method, like this:

     

    function getServerUrl()
    {
    	// Generate the URL schema using a basic check for SSL
    	var url = 'http' + (WEB_SERVER_PORT == 443 ? 's' : '') + '//';
    	
    	if (IS_SPLA)
    	{
    		// Add the Organisation name in the IFD format 
    		url += ORG_UNIQUE_NAME + '.';
    	}
    	
    	// Append the web server host
    	url += WEB_SERVER_HOST;
    	
    	// Add your custom URL suffix
    	url += '.mycompanyname.com'
    	
    	if (WEB_SERVER_PORT != 80 && WEB_SERVER_PORT != 443)
    	{
    		// Add the port if it is not standard HTTP or HTTPS
    		url += ':' + WEB_SERVER_PORT;
    	}
    	
    	if (IS_ONPREMISE)
    	{
    		// Add the on-premise organisation name
    		url += '/' + ORG_UNIQUE_NAME;
    	}
    	
    	// Return the URL
    	return url;
    }
    

     


    This takes into account IFD and on-premise environments. You can modify or simplify this as needed.

    Let me know if you need any more information.

     

    Thanks,
    Tully 

    Thursday, February 2, 2012 11:52 PM
  • Instead of writing a wrapper, here is what I do for all my JScript & Silverlight calls:

    var _url = "/" + Xrm.Page.context.getUniqueOrgName() + "/XRMServices/2011/OrganizationData.svc"

    It's worked like a charm and since I'm using a relative path I don't have to deal with the cross-domain pieces. 

    Jeremy

    Friday, February 3, 2012 12:49 AM