Getting Current User Profile Properties Using SharePoint REST API

In this blog, I have explained how to get the current user properties using AJAX call.

Introduction

In the demonstration, we get the current logged in user profile properties, like email, name, job title, profile URL, picture etc.

GetMyProperties

This property name helps to get the current user properties.

REST URL

http://<site url>/_api/sp.userprofiles.peoplemanager/<property name>

Let’s build the code to retrieve the current user properties.

Add jQuery reference.

Code

  1. <script src="https://sharepointtechie.sharepoint.com/sites/appnfc/SiteAssets/jquery-1.7.1.min.js" type="text/javascript"></script>  

Ajax Request

  1. var reqUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/GetMyProperties";   
  2. // Get the Api   
  3. $.ajax(  
  4.   {   
  5.     url: reqUrl, type: "GET", headers:   
  6.     {   
  7.       "accept""application/json;odata=verbose"   
  8.     },   
  9.     success: successHandler, error: errorHandler   
  10.   });   
  11. function successHandler(data)   
  12. {   
  13.   console.log(data);   
  14.   var Name = data.d.DisplayName;   
  15.   var email = data.d.Email;   
  16.   var oneUrl = data.d.PersonalUrl;   
  17.   var imgUrl = data.d.PictureUrl;   
  18.   var jobTitle = data.d.Title;   
  19.   var profUrl = data.d.UserUrl;   
  20.   var html = '';   
  21.   // Rendering the HTML structure html   
  22.   += "<li>Name: "+ Name +"</li>   
  23.     </br>   
  24.     <li>Email: "+ email +"  
  25.       </li>   
  26.       </br>   
  27.       <li>Personal: <a href ='"+ oneUrl +"'>One Drive</a></li> </br>  
  28.         <li>Profile Image: <img src ='" +imgUrl +"' alt='' width='40' height='40'></img></li></br>"; html += "<li>Job Title: "+ jobTitle +"</li>   
  29.           </br>  
  30.           <li>My Profile: <a href ="+ profUrl +">View my full profile</a></li>";   
  31.           $("#info").append(html);   
  32.   $('#new').attr('src', data.d.PictureUrl);   
  33. }   
  34. function errorHandler(error)   
  35. {   
  36.   alert(JSON.stringify(error));   
  37. }  

HTML Code

  1. <div id="Myprofileinfo"> <ul id="info"> <!—Append contents here --!> </ul> </div>  

Full code

  1. <script src="https://sharepointtechie.sharepoint.com/sites/appnfc/SiteAssets/jquery-1.7.1.min.js" type="text/javascript"></script>  
  2. <style type="text/css">  
  3.     li {  
  4.         list-style: none;  
  5.     }  
  6. </style>  
  7. <script>  
  8.     var reqUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/GetMyProperties";  
  9.     $.ajax({  
  10.         url: reqUrl,  
  11.         type: "GET",  
  12.         headers: {  
  13.             "accept""application/json;odata=verbose"  
  14.         },  
  15.         success: successHandler,  
  16.         error: errorHandler  
  17.     });  
  18.   
  19.     function successHandler(data) {  
  20.         console.log(data);  
  21.         var Name = data.d.DisplayName;  
  22.         var email = data.d.Email;  
  23.         var oneUrl = data.d.PersonalUrl;  
  24.         var imgUrl = data.d.PictureUrl;  
  25.         var jobTitle = data.d.Title;  
  26.         var profUrl = data.d.UserUrl;  
  27.         var html = '';  
  28.         html += "<li>Name: " + Name + "</li> </br> <li>Email: " + email + "</li> </br> <li>Personal: <a href ='" + oneUrl + "'>One Drive</a></li> </br> <li>Profile Image: <img src ='" + imgUrl + "' alt='' width='40' height='40'></img></li></br>";  
  29.         html += "<li>Job Title: " + jobTitle + "</li> </br><li>My Profile: <a href =" + profUrl + ">View my full profile</a></li>";  
  30.         $("#info").append(html);  
  31.         $('#new').attr('src', data.d.PictureUrl);  
  32.     }  
  33.   
  34.     function errorHandler(error) {  
  35.         alert(JSON.stringify(error));  
  36.     }  
  37. </script>  
  38. <!--- HTML -->  
  39. <div id="Myprofileinfo">  
  40.     <ul id="info"> </ul>  
  41. </div>  

Final Result


Conclusion

The above blog explained how to get the current logged in user profile properties. I hope it will be helpful for SharePoint professionals.

Happy SharePointing!