Search This Blog

Tuesday, March 1, 2016

SharePoint 2013: How to get Current User Information in REST Services

The future in SharePoint development is REST services; there will be very limited scope for server side code development. REST services provide various services to interact with SharePoint server and implement your business rules and logic. In this post we will learn how to fetch the information of current user like Login name, Email address, User Id, etc. We required this information frequently so let’s see how we can get this information.

The REST URL to get the User Information is:

http://[SiteUrl]/_api/web/CurrentUser

This service returns information like below:



JavsScript Code :

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

}

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/GetCurrentUser.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>


Happy SharePoint Coding.

No comments:

Post a Comment