locked
Retrieve regional settings by javascript RRS feed

  • Question

  • Is it possible to retrieve the current users regional settings by javascript, (Webservices or some object in the SP javascript). We need this to know if the current user uses DD/MM/YYYY or MM/DD/YYYY as dateformat.

    How did you guys fix this issue?


    MCT,MCTS - http://www.tomvangaever.be
    Thursday, August 20, 2009 10:46 AM

Answers

  • Hi,

    Guru Karnik: "You could try invoking the SharePoint User web service from javascript by passing the Current User Id and get the Regional Settings"
    Tom: "is it possible to retrieve the current users regional settings with the webservice?"

    I have serious doubts that one can get a user's regional settings (date format) using either Users and Groups web service (http://msdn.microsoft.com/en-us/library/ms772647.aspx) or UserProfileService (http://msdn.microsoft.com/en-us/library/websvcuserprofileservice.userprofileservice.aspx). I don't know wich one did Karnik mean by user web service.

    Probably the suggestion from Edin will do the job, but you should know the following limitation:
    "When you programmatically use the SPUser.RegionalSettings object on a server that is running Windows SharePoint Services 3.0, the value of the returned object is NULL" (http://support.microsoft.com/default.aspx/kb/944106)
    Since you have to check the regional settings for the current user, that may be no problem for you.

    An alternative approach requires no custom web service / assembly deployment on the server is to get the page http://yoursite/_layouts/regionalsetng.aspx?Type=User from JavaScript (for example using XmlHttpRequest object, http://www.w3schools.com/XML/xml_http.asp), and parse it for the
    select name="ctl00$PlaceHolderMain$ctl00$ctl00$DdlwebLCID"
    and there detect the value of the selected option, for example:
    <option selected="selected" value="1033">English (United States)</option>

    A bit of hacking, but may work well.

    Peter
    Saturday, August 22, 2009 9:59 PM
  • It works!!! Super!!!!

    var url = ctx.HttpRoot+"/_layouts/regionalsetng.aspx?Type=User";
    $.get(url,function(data){
        $(data).find("select[name=ctl00$PlaceHolderMain$ctl00$ctl00$DdlwebLCID]").find(":selected").each(function(){
            var lcid  = $(this).attr("value");
            var cultureInfo  = $(this).text();
        });
    });

    Thank you all very much for helping me!!!

    MCT,MCTS - http://www.tomvangaever.be
    Sunday, August 23, 2009 7:52 PM

All replies

  • You can make a custom web service , get the current SPUser and return the SPUser.RegionalSettings property. I think there's no OOTB client-only way to do this.
    -- Edin http://edinkapic.blogspot.com
    Saturday, August 22, 2009 9:07 AM
  • Saturday, August 22, 2009 9:51 AM
  • Deleted
    Saturday, August 22, 2009 1:13 PM
  • If you want to rely on the user's regional settings within the browser you can use the toLocaleString javascript method. http://www.tutorialspoint.com/javascript/date_tolocalestring.htm
    certdev.com
    Saturday, August 22, 2009 5:51 PM
  • We need to compare 2 dates with eachother in the RenderPattern of a custom field. We tried to do this with Javascript, but the Date class only accepts the USA format MM/DD/YYYY.

    this means that when a user changes his regional settings to DD/MM/YYYY, the date comparisation is wrong and we are showing the wrong KPI.

    @Karnik :  is it possible to retrieve the current users regional settings with the webservice?


    MCT,MCTS - http://www.tomvangaever.be
    Saturday, August 22, 2009 7:05 PM
  • Hi,

    Guru Karnik: "You could try invoking the SharePoint User web service from javascript by passing the Current User Id and get the Regional Settings"
    Tom: "is it possible to retrieve the current users regional settings with the webservice?"

    I have serious doubts that one can get a user's regional settings (date format) using either Users and Groups web service (http://msdn.microsoft.com/en-us/library/ms772647.aspx) or UserProfileService (http://msdn.microsoft.com/en-us/library/websvcuserprofileservice.userprofileservice.aspx). I don't know wich one did Karnik mean by user web service.

    Probably the suggestion from Edin will do the job, but you should know the following limitation:
    "When you programmatically use the SPUser.RegionalSettings object on a server that is running Windows SharePoint Services 3.0, the value of the returned object is NULL" (http://support.microsoft.com/default.aspx/kb/944106)
    Since you have to check the regional settings for the current user, that may be no problem for you.

    An alternative approach requires no custom web service / assembly deployment on the server is to get the page http://yoursite/_layouts/regionalsetng.aspx?Type=User from JavaScript (for example using XmlHttpRequest object, http://www.w3schools.com/XML/xml_http.asp), and parse it for the
    select name="ctl00$PlaceHolderMain$ctl00$ctl00$DdlwebLCID"
    and there detect the value of the selected option, for example:
    <option selected="selected" value="1033">English (United States)</option>

    A bit of hacking, but may work well.

    Peter
    Saturday, August 22, 2009 9:59 PM
  • @Peter, that is a brilliant hack indeed! I'll check it and post the solution right here...

    thanks for your help guys! If you know other possibilities don't hesitate to post! :)


    MCT,MCTS - http://www.tomvangaever.be
    Sunday, August 23, 2009 11:10 AM
  • Hi Tom,

    Thanks for the feedback, and waiting for the results.

    Peter
    Sunday, August 23, 2009 11:15 AM
  • It works!!! Super!!!!

    var url = ctx.HttpRoot+"/_layouts/regionalsetng.aspx?Type=User";
    $.get(url,function(data){
        $(data).find("select[name=ctl00$PlaceHolderMain$ctl00$ctl00$DdlwebLCID]").find(":selected").each(function(){
            var lcid  = $(this).attr("value");
            var cultureInfo  = $(this).text();
        });
    });

    Thank you all very much for helping me!!!

    MCT,MCTS - http://www.tomvangaever.be
    Sunday, August 23, 2009 7:52 PM
  • You will also need to check if the "Follow Web Settings" checkbox is checked, and if it is then get your value from _layouts/regionalsetng.aspx?Type=Site
    certdev.com
    Monday, August 24, 2009 1:36 AM
  • Hi Tom,

    Congrats, and thanks for sharing the code. It might help us too.

    Peter
    Monday, August 24, 2009 6:18 AM
  • @Steve: You don't need to check this, because it will automatically load the web settings in the combobox, so even if the checkbox is checked or not.. This solution will always give you the correct LCID..

    MCT,MCTS - http://www.tomvangaever.be
    Monday, August 24, 2009 10:26 AM
  • I am unable to make this work in my environment.  I can get the context and site.  I can make the request to the regional settings page and get back data.  However when I try to find the form field i'm not getting anything.  The only thing I can think of is that there is a limit to the amount of data that's being returned from the get.  I verified the form field name by viewing source from the regional settings page in my environment.  I'm running SharePoint 2010 SP1.

    Thoughts?

    Saturday, January 26, 2013 12:19 AM
  • The link you have provided is showing something else, it has nothing to do with getting regional settings.
    Thursday, September 21, 2017 5:54 AM