Monthly Archives: August 2011

How-To Series 9: Dynamic Ribbon Controls using CommandProperty – Infinite loop issue

There is nice article from MS Blog on creating dynamic ribbons in CRM 2011 http://blogs.msdn.com/b/crm/archive/2011/03/30/create-dynamic-ribbon-controls.aspx. I have followed the same approach to create a split button. However, here in my case we want to have the Menu XML dynamically generated based on some entity data. However, there was a problem when we tried to do that using ODATA query. There was nothing wrong in the code. ODATA query was able to retrieve the data and pass the result set to the call back method to populate ribbon split button. However, once the population is done in the code, control is moving back to starting point in the code(function DynamicMenu(CommandProperties)). It’s going into infinite loop.
function DynamicMenu(CommandProperties)
{
getData(CommandProperties, SuccessCallBack, ErrorCallBack);
}
function getData(CommandProperties, SuccessCallBack, ErrorCallBack)
{
//ODATA Stuff
success: function (data, textStatus, XmlHttpRequest)
        {
              // Success Call Back
       SuccessCallBack(CommandProperties,data);
        },
//ODATA Stuff
}
function SuccessCallBack(CommandProperties,data)
{
// Split Button population stuff
}
After bit(I can say a lot of) of struggle I felt it might be due to Asynchronous AJAX call in the script. To verify that I have commented ODATA call and kept “SetTimeOut(method(),4000)” in my code and did populate split button using static MenuXML. It’s again going to infinite loop. This time I have commented  “SetTimeOut” method too. Now, it’s working perfectly.
So, i thought of replacing ODATA Async Call with SOAP Synchronous call. This time it worked like a charm.….!!!!!! J
Hope it helps…!!!!
Cheers
Vikranth Pandiri

How To – Series 8: Cross Domain Issue while working with CRM 2011 Web Services in JScript

Back to blogging after long time. Recently I faced a “Cross Domain” issue while working with Jscript ODATA query. Below is the code which I have used:
    var ODATA_ENDPOINT = “/XRMServices/2011/OrganizationData.svc”;
    var context = GetGlobalContext();
    var serverUrl = context.getServerUrl();
   
    //Asynchronous AJAX function to Retrieve a CRM record using OData
    $.ajax({
        type: “GET”,
        contentType: “application/json; charset=utf-8”,
        datatype: “json”,
        url: serverUrl + ODATA_ENDPOINT + “/AccountSet?$filter=Address1_City eq ‘Redmond'”,
        beforeSend: function (XMLHttpRequest)
        {
            XMLHttpRequest.setRequestHeader(“Accept”, “application/json”);
        },
        success: function (data, textStatus, XmlHttpRequest)
        {
              // Success Call Back
        },
        error: function (XmlHttpRequest, textStatus, errorThrown)
        {
              // Error Call Back
        }
    });
The REST End point “url “ which I am passing in the above code is: http://<server>/<orgname>/XRMServices/2011/OrganizationData.svc/AccountSet?$filter=Address1_City eq ‘Redmond’.This code only works when we open CRM records using CRM “Server” name. It fails in other cases like when we open it using IP Address or localhost. The primary reason for this is due to “Cross Domain Policy”. The blocker is the REST End point which we are using. 
So, I thought of replacing server name with IP Address as follows.
I ran it in the browser…. It worked…!!!! J
So, I got a simple solution. 
Use the following instead of getting “ServerUrl” from the context object.
    var serverUrl = window.location.protocol + “//” + window.location.host + “/” + context.getOrgUniqueName();
Here we just need to use the window.location property to get the current window protocol and host and build the REST Endpoint.
Hope it helps…!!!! Please let me know if we have any other alternative solution. 
Cheers
Vikranth Pandiri.