Search This Blog

Tuesday, March 1, 2016

SharePoint 2013: How to get the user profile information of user using REST services

User profile services are common nowadays in SharePoint applications. Most of the applications uses User profile service to fetch user information. And in SharePoint 2013 it is recommended that we must create client side application rather than server side. So it is important to learn working with User Profile service using REST services.

Working with REST services is easy, just remember the REST URL and its parameters. User Profile service returns all properties of user if things goes right.

Following is the REST URL for getting properties of user

URL:  /_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='accountName'

Code snippets:

HTML code:

<script type="text/javascript" src="/Site%20Assets/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="/Site%20Assets/js/knockout.3.2.0.js"></script>
<script type="text/javascript" src="/Site%20Assets/js/GetUserfromService.js"></script>

<div>
Current USer Information:
<ul>
<li> 
Login name : <span data-bind="text: LoginName"></span>
</li>
<li> 
Title : <span data-bind="text: Title"></span>
</li>
<li> 
Email : <span data-bind="text: Email"></span>
</li>
</ul>
</div>

JS Code (GetUserfromService.js):


MY Code-

function GetLogedUserEmailID()
{
   var RESTURL = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties";
$.ajax({
url: RESTURL,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (userdata) {
  LogedUserEmailID = userdata.d.Email;
 
},
error: function (error) {
alert(JSON.stringify(error));
}
});


}

                                          or

 var RESTURL = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='accountName'";

$(document).ready(function () {
    GetUserInformation();
});
function GetUserInformation()
{
var self = this;
$.ajax({
url: RESTURL,
type: "GET",
headers: {
"accept": "application/json;odata=verbose",
},
success: function (userdata) {
   self.LoginName = userdata.d.AccountName;
self.Title  = userdata.d.DisplayName;
self.Email = userdata.d.Email;
ko.applyBindings(self);
},
error: function (error) {
alert(JSON.stringify(error));
}
});
}

Now we will see other various options available for User Profile services.

To get the properties of current user use following URL:

/_api/SP.UserProfiles.PeopleManager/GetMyProperties

To get the specific properties of user:

/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=LoginName

And

/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='FirstName,LastName')?@v='accountName'



In above code snippet we have just fetched some basic information. To fetch all other properties you need to iterate through properties array which is shown below.



In this way you can use REST services for User profile services interactions.


No comments:

Post a Comment