Get the Current Logged in User Using SharePoint 2013 CSOM

In the SharePoint 2013 environment, SP.UserProfiles JavaScript is used to get user profiles and user properties in custom solutions for SharePoint 2013 and apps for SharePoint.

There are various objects supported in the SP.userProfile namespace. They are given below.

  • HashTag
  • HashTagCollection
  • PeopleManager
  • PersonProperties
  • ProfileLoader
  • UserProfile
  • UserProfilePropertiesForUser

The following is sample code to get the current logged in user using the user profile in JavaScript.

<script src="/_layouts/15/SP.Runtime.js"></script>

<script src="/_layouts/15/SP.js"></script>

<script src="/_layouts/15/SP.UserProfiles.js"></script>

<script>

(function($){

$(document).ready(function(){

    // Ensure that the SP.UserProfiles.js file is loaded.

    SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js');

});

var userProfileProperties;

function loadUserData(){

    //Get Current Context

    var clientContext = new SP.ClientContext.get_current();

    // People Manager Instance

    var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

    // current user

    userProfileProperties = peopleManager.getMyProperties()

    clientContext.load(userProfileProperties);

    //Execute the Query.

    clientContext.executeQueryAsync(onSuccess, onFail);

}

function onSuccess() {

    alert(userProfileProperties.get_displayName());

}

function onFail(sender, args) {

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

  }

})(jQuery);

Up Next
    Ebook Download
    View all
    Learn
    View all