Showing posts with label Current user Language. Show all posts
Showing posts with label Current user Language. Show all posts

Friday, February 3, 2017

Change Language for current user with JSOM in SharePoint Online

Hi,

Here is code which can change language for current user with JSOM :


<script type="text/javascript" charset="utf8" src="/sites/BizDev/Style%20Library/SPS/jquery-1.11.3.min.js"></script>
<select id='LangSelect' onchange="SetUserLanguage(this.value);"></select>

<script type="text/javascript">

function SetUserLanguage(lCode)
{
    var call = $.ajax({
            url: _spPageContextInfo.siteAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Languages')/items?" + "$select=Title,Code,LCode,ID&$top=5000&$filter=Code eq "+lCode,
            type: "GET",
            dataType: "json",
            headers: {
                Accept: "application/json;odata=verbose"
            }
        });

        call.done(function (data, textStatus, jqXHR) {
            var multipleValues = [];
            for (var i=0;i<data.d.results.length;i++){
                console.log(data.d.results[i].LCode);
               multipleValues.push(data.d.results[i].LCode);
            }
            //setTimeout(Blank, 2000);
           
            console.log(lCode);
            SP.SOD.executeFunc("sp.js", "SP.ClientContext", function(){
            SP.SOD.registerSod("sp.userprofiles.js", SP.Utilities.Utility.getLayoutsPageUrl("sp.userprofiles.js"));
            SP.SOD.executeFunc("sp.userprofiles.js", "SP.UserProfiles.PeopleManager", SetCurrentUserProperties);
            });
       
        var userProfileProperties;
       
        function SetCurrentUserProperties(){
           
            console.log( typeof lCode);
            //Get Current Context   
            var clientContext = SP.ClientContext.get_current();
           
            //Get Instance of People Manager Class
            var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
           
            //Get properties of the current user
            userProfileProperties = peopleManager.getMyProperties();
           
            //Get only the accountname instead of all the properties.
            clientContext.load(userProfileProperties, "AccountName");
           
            //Execute the Query.
            clientContext.executeQueryAsync(function(){
               
                //Get the account name of the current user. It will be in the following format: "i:0#.f|membership|username@yoursite.onmicrosoft.com"
                var currentUserAccountName = userProfileProperties.get_accountName();
               
                console.log(currentUserAccountName);
                //Set a single value property
                peopleManager.setSingleValueProfileProperty(currentUserAccountName, "SPS-Locale", lCode);
               
                //Set a multivalue property
                //var multipleValues = ["SharePoint", "Office 365", "Architecture"];
                console.log(multipleValues);
                peopleManager.setMultiValuedProfileProperty(currentUserAccountName, "SPS-MUILanguages", multipleValues);
               
                peopleManager.setSingleValueProfileProperty(currentUserAccountName, "AboutMe", "Value updated from JSOM!");
                //Execute the Query.
                clientContext.executeQueryAsync(function(){
                   
                    console.log("properties updated!");
                },
                function(sender,args){
                    //On Error
                    console.log(args.get_message());
                });
               
            }, function(sender,args){
                //On Error
                console.log(args.get_message());
            });
           
        }
    });

}

</script>