Wednesday, February 25, 2015

CRM 2015 Xrm.Page.context has null values

Recently I stumbled across an issue with an On-Premise, IFD deployment of CRM 2015. The issue involved the Xrm.Page.context not being completely populated when being used on an HTML web resource. Upon further investigation I found that a strange redirect was occurring with CRM 2015 when loading the ClientGlobalContext.js.aspx as a script in the HTML header.
In previous versions of CRM, adding the script tag to load ClientGlobalContext.js.aspx was straight forward and would load the context with out issue. However, in CRM 2015, you will see that the same ClientGlobalContext.js.aspx is loaded, then another one gets requested.  
As you can see in the picture above, the second request receives a 404 because the URL contains the organization name and is not formatted correctly for IFD deployments.
If we dive deeper we can see what is happening in the first call:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/javascript; charset=utf-8
Expires: -1
Vary: Accept-Encoding
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
REQ_ID: 190d26df-27ea-4383-a166-0b3c2af636c5
X-Powered-By: ASP.NET
Date: Wed, 25 Feb 2015 19:32:42 GMT
Content-Length: 822

document.write('<script type="text/javascript" src="'+'\x2f_common\x2fglobal.ashx\x3fver\x3d698467308'+'"></'+'script>')
document.write('<script type="text/javascript" src="'+'\x2f_common\x2fwindowinformation\x2fwindowinformation.js.aspx\x3flcid\x3d1033\x26amp\x3bver\x3d698467308'+'"></'+'script>')
document.write('<script type="text/javascript" src="'+'\x2f_common\x2fentityproperties\x2fentitypropertiesutil.js.aspx\x3ftstamp\x3d1469436481\x26amp\x3bver\x3d698467308'+'"></'+'script>')
function GetGlobalContext(){return Xrm.Page.context};var xhr = new XMLHttpRequest();
xhr.open("GET", "/<OrgName>/WebResources/ClientGlobalContext.js.aspx", false);
xhr.addEventListener('load',function(){if(xhr.status == 200){eval(xhr.responseText);}});
xhr.setRequestHeader("Content-Type", "application/json");xhr.send();
As you can see the highlighted line is the cause of the problem. The ClientGlobalContext.js.aspx that we are used to using is now attempting to redirect to a different ClientGlobalContext.js.aspx that loads up all of the goodies that we are used to (getUserID, getOrgName, etc).

According to this thread, Microsoft is already aware of the problem and intends on fixing it in UR1 (Release date: TBD). Unfortunately, there may be some users already experiencing this issue in production environments. For those users, I would suggest using the workaround provided by Yona Low in the same thread. I will quote it below:
A temporary workaround we implemented is to add a URL Rewrite rule in IIS to remove the org name from the URL, if it exists when requesting ClientGlobalContext.js.aspx. The rule needs to be added before the built-in CRM rules.
Hopefully this will be fixed in the next UR.
Here are the values used:
Pattern: /?([0-9a-zA-Z][^/]*)?/?((?:%7b|\{)[^/]*(?:%7d|\}))?/?WebResources/ClientGlobalContext.js.aspx
Condition: {R:1} matches .+
Action: Rewrite to /WebResources/ClientGlobalContext.js.aspx
The above URL Rewrite rule will only work for IFD-only deployments. It has not been tested and will probably not work for mixed deployments.
Here is a screenshot of the settings:
I have implemented this workaround successfully, and I hope that it will also help you. Please follow the Microsoft Connect Feedback item related to this bug, and possibly comment on it as well.

This has also been posted on CRMUG.

3 comments:

  1. Microsoft Dynamics CRM 2015 Update 0.1 (KB 3010990) has addressed this issue

    ReplyDelete
  2. I’m impressed, I have to admit. Rarely do I encounter a blog that’s both educative and interesting, and without a doubt, you’ve hit the nail on the head. The problem is something which too few people are speaking intelligently about. I am very happy I stumbled across this in my hunt for something relating to this.
    top private equity firms

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete