Get Current Login User Details using Rest API in SharePoint 2013

//Below code will get the current user id from the Site
var currentUserID = _spPageContextInfo.userId;

//this function get the user details from current user id
$(document).ready(function()
{
var success = getUser(currentUserID);
alert(success );
});


function getUser(id){
var deferred = $.Deferred();
var serverUrl = _spPageContextInfo.webAbsoluteUrl;
var ListNameUrl = String.format(
"{0}/_api/Web/GetUserById(" + id + ")",serverUrl);
jQuery.ajax({
url: ListNameUrl,
type: "GET",
headers: { "Accept": "application/json;odata=verbose" },
success: function(data) {
var dataResults = data.d;
//get login name  
var loginName  = dataResults.LoginName.split('|')[1];
alert(loginName);     
//get display name
displayName = dataResults.Title;
deferred.resolve();
}
});
return deferred.promise();
}

Comments

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

Add content type to SharePoint List/Library using REST API

A type named 'SP.Data. could not be resolved by the model error