How to Get User Profile Properties in SharePoint 2013 using JavaScript


Sathish Nadarajan
SharePoint MVP
Published On :   13 Dec 2015
Visit Count
Today :  14    Total :   13578
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


In one of the recent requirement, I was about to render the User Profile Information on an Item Display Template. In that case, I don’t have any other option apart from querying the User Profile Service Application using the PeopleManager Class from the JavaScript. Let us have a quick close look on it.

 $.ajax({
         asynch: false,
         url: location.protocol + "//" + location.host + "/SiteCollectionURL/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='" + username + "'",
         method: "GET",
         headers: { "Accept": "application/json; odata=verbose" },
         success: function (data) {
          
         if (data.d.UserProfileProperties.results.length > 0) {
                     for (var i = 0; i < data.d.UserProfileProperties.results.length; i++) {
                        
                         if (data.d.UserProfileProperties.results[i].Key === "WorkPhone") { workPhone = data.d.UserProfileProperties.results[i].Value; }
                         if (data.d.UserProfileProperties.results[i].Key === "Title") { designation = data.d.UserProfileProperties.results[i].Value; }
                         if (data.d.UserProfileProperties.results[i].Key === "FirstName") { firstName = data.d.UserProfileProperties.results[i].Value; }
                         if (data.d.UserProfileProperties.results[i].Key === "LastName") { lastName = data.d.UserProfileProperties.results[i].Value; }
                         if (data.d.UserProfileProperties.results[i].Key === "WorkEmail") { email = data.d.UserProfileProperties.results[i].Value; }
     
                         
                     }
                 }
     if (data.d.PictureUrl !== null) { photo = data.d.PictureUrl; }
 
      
 
     },
     error: function (x, y, z) {
                 alert(JSON.stringify(x) + '\n' + JSON.stringify(y) + '\n' + JSON.stringify(z));
             }
 
         });
 

The only key thing here is, we cannot retrieve directly by using the Key. We need to loop through the available properties and make sure that the Key is equal to our expected Property, then take the Value.

If someone knows a method directly fetching that, please post it here.

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint