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 !!

 

Advertisements

5 Comments

Filed under MS CRM 2011

5 responses to “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 ??

  2. Karan

    Hi Mahendar

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

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

  3. mahenderpal

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

  4. Karan

    I tried what u said.. I am getting a Login screen asking my login credentials..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s