SharePoint 2013: How to get user profile and user information in JavaScript or JSOM.

SharePoint 2013: How to get user profile and user information in JavaScript or JSOM.

 

Getting user profile or user information of a specific or current user in SharePoint 2013 is very easy. SharePoint 2013 has provided client side object model library for the user profile activities; which enables deep interaction with user profile. For JavaScript side object model you need to load following .js file in order to start coding against User profile: "SP.UserProfiles.js".

In this article we will see how to fetch the user profile information of a specific or current user inside JavaScript/ client side.

 

First of all you need to load the “SP.UserProfiles.js, SP.Runtime.js and SP.js” js files on your SharePoint page, use following code snippet to load these files,

 

$(document).ready(function () {
 
    var scriptbase = _spPageContextInfo.webAbsoluteUrl + "/_layouts/15/";
 
    $.getScript(scriptbase + "SP.Runtime.js",
 
        function () {
 
            $.getScript(scriptbase + "SP.js",
 
               function () {
 
                   $.getScript(scriptbase + "SP.UserProfiles.js", GetUserInformation);
 
               });
 
        });
 
 
 
});
 

Here I am using “$(document).ready”, so you need to refer “jquery-1.6.2.min.jsalso. Now once the script files loaded successfully your code is ready to go. So to fetch the user profile, first of all

get the current client context. After that you need initialize the ‘SP.UserProfiles.PeopleManager’ object. This is the class which provides all the information regarding user profile. The below code snippet gives you the user profile Information.

 

var userProfileProperties;
 
function GetUserInformation() {
 
 // Get the current client context.
 
        var clientContext = SP.ClientContext.get_current();
 
        //Get Instance of People Manager Class   
 
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
        var targetUser = "domain\\userId";
        userProfileProperties = peopleManager.getPropertiesFor(targetUser);
 
  
 
        //Execute the Query.  
 
        clientContext.load(userProfileProperties);
 
        clientContext.executeQueryAsync(onSuccess, onFail);    }
 
  
 
    function onSuccess () {
 
        // Display user information from userProfileProperties.
 
    }
 
  
 
    function onFail (sender, args) {
 
        alert("Error: " + args.get_message());
 
    }
 

So inside “onSuccess()” method you can get full information of user. In this way can get the User profile of person at client side.

Leave a Comment
  • Please add 1 and 5 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Gokan Ozcifci edited Original. Comment: C# code added

Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Gokan Ozcifci edited Original. Comment: C# code added

Page 1 of 1 (1 items)