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>

Change Site Language with JSOM

Hi,

Here is code which can change site language with JSOM :

<script type="text/javascript" charset="utf8" src="/Style%20Library/SPS/jquery-1.11.3.min.js"></script>

<select id='LangSelect' onchange="SetLanguage();"></select>


<script type="text/javascript">


$( document ).ready(function() {
    LoadLanguages();
    setTimeout(GetUpdatedLangulage, 2000);
});

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

        call.done(function (data, textStatus, jqXHR) {
            var option = '';
            for (var i=0;i<data.d.results.length;i++){
               option += '<option value="'+ data.d.results[i].Code + '">' + data.d.results[i].Title + '</option>';
            }
            $('#LangSelect').append(option);
        });
}

function SetLanguageInRegionalSettings()
{
    var context = SP.ClientContext.get_current();

    var web = context.get_web();

    var regionalSettings = web.get_regionalSettings();

    context.load(regionalSettings)

    context.executeQueryAsync(function () {

            var sel = document.getElementById('LangSelect');
            console.log( sel.value );

            regionalSettings.set_localeId(sel.value); //English (UK)
   
            regionalSettings.update();
   
            context.executeQueryAsync(function () {
                console.log("success");
            }, function (sender, args) {
                console.log(args.get_message());
            });
        }, function (sender, args) {
            console.log(args.get_message());
    });
}

function GetUpdatedLangulage()
{
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    var regionalSettings = web.get_regionalSettings();
    context.load(regionalSettings,  "LocaleId");

    context.executeQueryAsync(function () {

        //Locale Id
        console.log(regionalSettings.get_localeId()); //2057
        var sel = document.getElementById('LangSelect');
        sel.value = regionalSettings.get_localeId();
    }, function (sender, args) {
        console.log(args.get_message());
    });
}


function SetLanguage()
{
    var context = SP.ClientContext.get_current();

    var web = context.get_web();

    //This is important. Wont work otherwise.
    web.set_isMultilingual(true);
   
    var sel = document.getElementById('LangSelect');
    console.log( sel.value );
    web.addSupportedUILanguage(sel.value); //Dutch
   
    //The full list of SharePoint 2013 and SharePoint Online LCIDs is here:
    //https://technet.microsoft.com/en-us/library/ff463597.aspx
   
    web.update();
   
    context.executeQueryAsync(function(){
        console.log("Language Added");
        SetLanguageInRegionalSettings();
    },function(sender,args){
        console.log(args.get_message());
        SetLanguageInRegionalSettings();
    });   
}

</script>

Remove App from SharePoint Online with Powershell

Hi,

Today I was facing some issues in installed custom app. I was not able to get App Details & also not able to remove the app. I tried alot from interface but I couldn't remove the app. Then I move to Powershell scripts.

Here is the powershell script which can remove the app from SharePoint Online. Only prerequisite is you must have SharePoint Online Client Components SDK installed on the system.

cls
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")


Function Get-ClientContext([string]$Url,[string]$UserName,[string]$Password)
{
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
    $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $Password)
    return $context
}


Function Uninstall-AppInstance([Microsoft.SharePoint.Client.ClientContext]$Context,[Guid]$AppInstanceId)
{
    $appInst = $Context.Web.GetAppInstanceById($AppInstanceId)
    $appInst.Uninstall()
    $context.ExecuteQuery()
 }




$UserName = "gaurav.goyal@myorgs.com"
$Password=Read-Host -Prompt "Password" -AsSecureString;

$Url = "https://mytenant.sharepoint.com"
$AppInstanceid = New-Object Guid("XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")  #you need to specify App Instance Id here

$context = Get-ClientContext -Url $Url -UserName $UserName -Password $Password

Uninstall-AppInstance -Context $context -AppInstanceId $AppInstanceid
$context.Dispose()