Get Current Logged in User and Display Name using SharePoint 2013 REST API

This SharePoint Rest API tutorial explains, how to get current logged in user details like display name, email, etc using Rest API in SharePoint 2013/2016 or SharePoint Online.

In this Article explore with REST API is quite simple and straightforward, used User ID to get user Title, Email for SharePoint 2013 and apps for SharePoint. use /_api/web/getuserbyid(ID) to get the user field in the response data, we have an “AuthorId” will get us the user Title, Email etc.

Get Current Logged in User and Display Name using SharePoint 2013 REST API
Get Current Logged in User and Display Name using SharePoint 2013 REST API

Get Current Logged in User and Display Name using SharePoint 2013 REST API

Step-1: Navigate to your SharePoint 2013 site and create a Wiki Page or a Web Parts page.

Step-2: The process to add your JavaScript code is quite straightforward:
Edit the page, go to the “Insert” tab in the Ribbon and click the “Web Part” option. In the “Web Parts” picker area, go to the “Media and Content” category, select the “Script Editor” Web Part and press the “Add button”.

Step-3: Once the Web Part is inserted into the page, you will see an “EDIT SNIPPET” link click. You can insert the HTML and/or JavaScript code into the dialog.

get current user details using rest api in SharePoint 2013
get current user details using rest api in SharePoint 2013
<script src=”/Style Library/scripts/jquery-1.10.1.min.js”></script>
<script type=”text/javascript”>
var userid= _spPageContextInfo.userId;
var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/getuserbyid(” + userid + “)”;
var requestHeaders = { “accept” : “application/json;odata=verbose” };
$.ajax({
url : requestUri,
contentType : “application/json;odata=verbose”,
headers : requestHeaders,
success : onSuccess,
error : onError
});

function onSuccess(data, request){
var Logg = data.d;
//get login name
var loginName = Logg.LoginName.split(‘|’)[1];
alert(loginName);
//get display name
alert(Logg.Title);
}
function onError(error) {
alert(“error”);
}
</script>

In the code above, we’ll get the author id from the list. By passing the author id in to GetUserBuId() method, it will return the user in raw format like “I:O#.f|xxxx|xx@xxxx.com”. We can get the login name by splitting the output.

get current user details using rest api in SharePoint online
get current user details using rest api in SharePoint online

Get Current User Details Using SharePoint 2013 JavaScript

This JavaScript tutorial, we will discuss how to retrieve current user details using SharePoint 2013 CSOM using JavaScript Object model (jsom).

In SharePoint 2013 environment, SP.UserProfiles javascript is used to get user profiles and user properties in custom solutions for SharePoint 2013 and apps for SharePoint.

There are various object supported in SP.userProfile namespace. They are given as below.

  • HashTag
  • HashTagCollection
  • PeopleManager
  • PersonProperties
  • ProfileLoader
  • UserProfile
  • UserProfilePropertiesForUser

In this article, I will show the sample code to get the current logged in user using user profile JavaScript.

<script src="/_layouts/15/SP.Runtime.js"></script>
<script src="/_layouts/15/SP.js"></script>
<script src="/_layouts/15/SP.UserProfiles.js"></script>

(function($){

$(document).ready(function(){
// Ensure that the SP.UserProfiles.js file is loaded.
SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, ‘SP.UserProfiles.js’);
});

var userProfileProperties;

function loadUserData(){

//Get Current Context
var clientContext = new SP.ClientContext.get_current();

// People Manager Instance
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);

// current user
userProfileProperties = peopleManager.getMyProperties()

clientContext.load(userProfileProperties);

//Execute the Query.
clientContext.executeQueryAsync(onSuccess, onFail);

}

function onSuccess() {
alert(userProfileProperties.get_displayName());
}

function onFail(sender, args) {
alert(“Error Occured: " + args.get_message());
}
})(jQuery);

You may like following SharePoint Rest API tutorials:

Hope this SharePoint tutorial helps to get current logged in user details like display name, email etc using Rest API in SharePoint 2013/2016 or SharePoint Online.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>