Monday, January 7, 2013

MS CRM SDK 5.0.13: Xrm.Page.context.getClientUrl - at last!

The long long wait is over...

Have you ever had a CRM installation that can be accessed using different URLs, e.g. one to be used inside a corporate network, and one to be used for external access?
Have you also written some javascript to make calls to the OData endpoint or to open a new entity form?

Then you have realized that the context.getServerUrl method returns the URL that was assigned the server during the installation of CRM, and not the URL currently used to access CRM.

Why is that a problem?

Well, authenticating the user to one URL, and then having javascript making calls to the OData endpoint on another URL, will just give the user a "too bad, you are not authorized to do anything on this URL".

Now, at last, in Microsoft Dynamics CRM Software Development Kit version 5.0.13 the context.getClientUrl method is introduced, which returns the base URL currently used by the user to access the CRM application.
The getServerUrl method is still there, but deprecated and should not be used in the future.

Documentation can be found here.

Happy javascripting!

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This is a great post! Unfortunately we are still on UR11 for a while. Something to add if anyone is having the same issue, window.location.protocol + "//" + window.location.host + "/" + Xrm.Page.context.getOrgUniqueName(); will at least help you build a URL from scratch.

    ReplyDelete