Top Menu

Showing posts with label JSOM. Show all posts
Showing posts with label JSOM. Show all posts

Friday, October 4, 2013

User Profile Properties through JSOM

Following is the code to get the current user's Profile Properties through JSOM (JavaScript Object Model) in SharePoint 2013.
<script src="/_layouts/15/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="/_layouts/15/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/_layouts/15/init.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.runtime.js" type="text/javascript"></script>
<script src="/_layouts/15/sp.js" type="text/javascript"></script>
<script src="/_layouts/15/SP.UserProfiles.js" type="text/javascript"></script>

<script type="text/javascript">

    //$(document).ready(function(){
        SP.SOD.executeOrDelayUntilScriptLoaded(getUserProperties, 'SP.UserProfiles.js');
    //});

    var userProfileProperties;

    function getUserProperties() {

        var clientContext = new SP.ClientContext.get_current();
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
        userProfileProperties = peopleManager.getMyProperties();
        clientContext.load(userProfileProperties);
        clientContext.executeQueryAsync(onRequestSuccess, onRequestFail);
    }

    // This function runs if the executeQueryAsync call succeeds.
    function onRequestSuccess() {
        var messageText = "<b>";
        if (userProfileProperties.get_userProfileProperties()['Title'] != "")
            messageText += userProfileProperties.get_userProfileProperties()['Title'];
        if (userProfileProperties.get_userProfileProperties()['SPS-Department'] != "")
            messageText += ", " + userProfileProperties.get_userProfileProperties()['SPS-Department'];
        if (messageText.length > 5)
            messageText += "<br/>";
        if (userProfileProperties.get_userProfileProperties()['Office'] != "")
            messageText += userProfileProperties.get_userProfileProperties()['Office'];
        if (userProfileProperties.get_userProfileProperties()['WorkPhone'] != "")
            messageText += ", " + userProfileProperties.get_userProfileProperties()['WorkPhone'];
        messageText += "</b>";
        $get("results").innerHTML = messageText;
    }

    // This function runs if the executeQueryAsync call fails.
    function onRequestFail(sender, args) {
        $get("results").innerHTML = "Error: " + args.get_message();
    }

</script>

<div id="results"></div>

Thursday, April 4, 2013

SharePoint Online - Change Master Page through CSOM

Scenario:
I had a requirement where one of our clients have a Team Site in SharePoint Online (Office 365) environment and the master page of that Team Site needs to be changed. 

Now the problem is _layouts/15/ChangeSiteMasterPage.aspx is not available in the Team Sites through Site Settings and Server Side code is not an option in SharePoint Online environment because Microsoft does not allow any server side deployment.

Solution:
The amazing Client-Side Object Model (JavaScript Object Model to be exact) saved the day. Here is the code:
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script> 
<script type="text/javascript" src="/_layouts/15/sp.js"></script> 
<script type="text/javascript">
    function ChangeMasterPage() {

        var context;
        var web;
        var strMasterPageUrl = '/_catalogs/masterpage/my_custom.master';

        context = new SP.ClientContext.get_current();
        web = context.get_web();

        web.set_customMasterUrl(strMasterPageUrl);
        web.set_masterUrl(strMasterPageUrl);
        web.update();

        context.executeQueryAsync(function () {

            alert("Master Page has been set to \n" + strMasterPageUrl);

        }, function (sender, args) {

            alert("Error: " + args.get_message());

        });
    }

    ChangeMasterPage();
</script> ​​​​ ​​​​

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...