web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / xRMCenter / Getting TimeZone With Micro...

Getting TimeZone With Microsoft CRM Web API 2016

nghieppham Profile Picture nghieppham 4,755

In this post, we will get the Time zone of current user via Microsoft Dynamic API. Microsoft Dynamic CRM has supported many functions, you can refer this link. This post will use GetAllTimeZonesWithDisplay to demonstrate it. 

Note: Your web API will look like  Your CRM Url + “/api/data/v8.0/” (Version is depend on your Microsoft Dynamic CRM)

Step 1: Creating 1 js web resource libCrm.js  

step 2: Creating invokeUnboundFunction function as following

function invokeUnboundFunction (functionNameparameters, successCallback, errorCallbackcallerId) {

    ///<summary>Invoke an unbound function</summary>
    /// <param name=”functionName“type=”String”>The name of the unbound function you want to invoke</param>
    /// <param name=”parameters” type=”Array”>An array of strings representing the parameters to pass to the unbound function</param>
    /// <param name=”successCallback” type=”Function”>The function to call when the function is invoked. The results of the unbound function will be passed to this function.</param>
    /// <param name=”errorCallback” type=”Function”>The function to call when there is an error. The error will be passed to this function.</param
    /// <param name=”callerId” type=”String” optional=”true”>The systemuserid value of the user to impersonate</param>
 
    var UriPath = “Your crm Web Api” + functionName;
    var parameterNames = [];
    var parameterAliasValues = [];
    var parameterNumber = 1;
    if (parameters) {
        parameters.forEach(function (param) {
            var keyValue = param.split(“=”);
            var name = keyValue[0];
            var value = keyValue[1];
            parameterNames.push(name + “=” + “@p” + parameterNumber.toString());
            parameterAliasValues.push(“@p” + parameterNumber.toString() + “=” + value)
            parameterNumber++;
        });
        UriPath = UriPath + “(“ + parameterNames.join(“,”) + “)?” +          parameterAliasValues.join(“&”);
    }
    else {
        UriPath = UriPath + “()”;
    }
 
    var req = new XMLHttpRequest();
    req.open(“GET”, encodeURI(UriPath),
true);
    req.setRequestHeader(“Accept”, “application/json);
    if (callerId) {
        req.setRequestHeader(MSCRMCallerID, callerId);
    }
    req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8″);
    req.setRequestHeader(“OData-MaxVersion, “4.0”);
    req.setRequestHeader(“OData-Version”, “4.0”);
    req.onreadystatechange = function() {
        if (this.readyState == 4 /* complete */) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                if (successCallback)
                    successCallback(JSON.parse(this.response, dateReviver));
            }
            else {
                if (errorCallback)
                    errorCallback(errorHandler(this));
            }
        }
    };
    req.send();
}
Step 2: creating dateReviver method (It is not required)
function dateReviver(key, value) {
  var a;
  if (typeof value === ‘string’) {
   a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
   if (a) {
    return new Date(Date.UTC(+a[1],
+a[2] – 1, +a[3], +a[4], +a[5], +a[6]));
   }
  }
  return value;
 };
Step 3: You can create GetLocalTimeZone to  call invokeUnboundFunction
function GetLocalTimeZone getLocalTimeZone(lcidParam) {
var lcidParam = [LocaleId=1033″];
  invokeUnboundFunction(“GetAllTimeZonesWithDisplayName”,//functionName
  lcidParam,
//parameters
  GetAllTimeZonesWithDisplayNameSuccess,
//successCallback
  sampleErrorHandler);//errorCallback
}
Note: You can get LocaleId from context of CRM user like
Xrm.Page.context.getOrgLcid()
You can define the errorHander function like following method.
function errorHandler (resp) {
  switch (resp.status) {
   case 503:
    return new Error(resp.statusText
+
” Status Code:” + resp.status + ” The Web API Preview is not enabled.”);
    break;
   default:
    return new Error(“Status Code:” +
resp.status +

+ parseError(resp));
    break;
  }
 }
Step 4: You can create 1 custom button, add crmlib.js and add GetLocalTimeZone as Action.
Hope it will be useful for someone.


This was originally posted here.

Comments

*This post is locked for comments