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
- <script src="https://sharepointtechie.sharepoint.com/sites/appnfc/SiteAssets/jquery-1.7.1.min.js" type="text/javascript"></script>
Ajax Request
- var reqUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/GetMyProperties";
-
- $.ajax(
- {
- url: reqUrl, type: "GET", headers:
- {
- "accept": "application/json;odata=verbose"
- },
- success: successHandler, error: errorHandler
- });
- function successHandler(data)
- {
- console.log(data);
- var Name = data.d.DisplayName;
- var email = data.d.Email;
- var oneUrl = data.d.PersonalUrl;
- var imgUrl = data.d.PictureUrl;
- var jobTitle = data.d.Title;
- var profUrl = data.d.UserUrl;
- var 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>"; html += "<li>Job Title: "+ jobTitle +"</li>
- </br>
- <li>My Profile: <a href ="+ profUrl +">View my full profile</a></li>";
- $("#info").append(html);
- $('#new').attr('src', data.d.PictureUrl);
- }
- function errorHandler(error)
- {
- alert(JSON.stringify(error));
- }
HTML Code
- <div id="Myprofileinfo"> <ul id="info"> <!—Append contents here --!> </ul> </div>
Full code
- <script src="https://sharepointtechie.sharepoint.com/sites/appnfc/SiteAssets/jquery-1.7.1.min.js" type="text/javascript"></script>
- <style type="text/css">
- li {
- list-style: none;
- }
- </style>
- <script>
- var reqUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/GetMyProperties";
- $.ajax({
- url: reqUrl,
- type: "GET",
- headers: {
- "accept": "application/json;odata=verbose"
- },
- success: successHandler,
- error: errorHandler
- });
-
- function successHandler(data) {
- console.log(data);
- var Name = data.d.DisplayName;
- var email = data.d.Email;
- var oneUrl = data.d.PersonalUrl;
- var imgUrl = data.d.PictureUrl;
- var jobTitle = data.d.Title;
- var profUrl = data.d.UserUrl;
- var html = '';
- 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>";
- html += "<li>Job Title: " + jobTitle + "</li> </br><li>My Profile: <a href =" + profUrl + ">View my full profile</a></li>";
- $("#info").append(html);
- $('#new').attr('src', data.d.PictureUrl);
- }
-
- function errorHandler(error) {
- alert(JSON.stringify(error));
- }
- </script>
- <!--- HTML -->
- <div id="Myprofileinfo">
- <ul id="info"> </ul>
- </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!