Getting Current User Date and Time Format setting using Javascript MS CRM 2011

If you are looking to get current user Date and Time format you can use below code for the same

function RetrieveUserSettingRecord() {

 var context;

 var serverUrl;

 var ODataPath;

context = Xrm.Page.context; //get context

 serverUrl = context.getServerUrl();

 ODataPath = serverUrl +“/XRMServices/2011/OrganizationData.svc”;

 var UserID = Xrm.Page.context.getUserId();  //get current user id from context

 var RetrieveUserSetting = new XMLHttpRequest();

RetrieveUserSetting.open(“GET”, ODataPath + “/UserSettingsSet(guid'” + UserID + “‘)”, true);

RetrieveUserSetting.setRequestHeader(“Accept”, “application/json”);

RetrieveUserSetting.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);

RetrieveUserSetting.onreadystatechange =function() {

RetrieveUserSettingCallBack(this);

};

 RetrieveUserSetting.send();

 }

 function RetrieveUserSettingCallBack(retrievedUserSetting) {

 if (retrievedUserSetting.readyState == 4 /* complete */) {

 if(retrievedUserSetting.status == 200) {

 var retrievedUser = this.parent.JSON.parse(retrievedUserSetting.responseText).d;

 if (retrievedUser.TimeFormatString != null)

alert(retrievedUser.TimeFormatString);

 if (retrievedUser.DateFormatString != null)

alert(retrievedUser.DateFormatString);

 }}

 }

 Enjoy !!

 

6 thoughts on “Getting Current User Date and Time Format setting using Javascript MS CRM 2011

  1. Karan

    Hi Mahendar

    Thanks for the great help.

    I have tried with above code in the onload of my custom entity.
    I am getting ‘Access is denied’. Any idea why I am getting this ??

    Reply
    1. mahenderpal Post author

      you are welcome Karan,

      Did you try to debug your code to see on which line you are getting this error ??

      Reply
  2. Karan

    Hi Mahendar

    I am getting exception “Access is denied” on the following line:

    RetrieveUserSetting.open(“GET”, ODataPath + “/UserSettingsSet(guid'” + UserID + “‘)”, true);

    Reply
  3. mahenderpal Post author

    This is strange, you should not get this error, are you able to browse UserSettingsSet directly from browser ?? using below url
    http://[Your Organization Root URL]/XRMServices/2011/OrganizationData.svc/UserSettingsSet(guid’xxxxxxxxxxxxxx’) //replace guid

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *