SharePoint 2016: Get Current User Using REST API

REST API In SharePoint

In this post, I will explain How to

Get Current SharePoint User using REST API.

REST API In SharePoint

You may be also interested to read SharePoint 2016: Get Current User Using JavaScript.


REST API In SharePoint

  • REST API stands for Representational State Transfer.
  • It exposes all the SharePoint entities included in the SharePoint Client APIs.
  • REST API doesn’t require to reference any SharePoint assemblies.
  • It makes an HTTP request to GET or POST or PUT or DELETE sharepoint objects like webs, lists …etc.

For more details, Please check Get to know the SharePoint REST service.


Get Current SharePoint User Using REST API

In this sample, I’ll use the REST API endpoint “/_api/web/CurrentUser” to get Current SharePoint User:

  • Login Name.
  • Display Name.
  • Email.

Steps

  • Edit your page.
  • Add Script Editor Web Part.
Script Editor Webpart
  • Add the below script.
< script type = "text/javascript" src ="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>


	<script type = "text/javascript">

	$(document).ready(function () {
		GetUserInfoRESTAPI();
	});

function GetUserInfoRESTAPI() {

	var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/CurrentUser";
	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) {

		alert('LoginName:' + data.d.LoginName);
		alert('LoginName:' + data.d.LoginName.substring(data.d.LoginName.indexOf('|') + 1));
		alert('Display Name:' + data.d.Title);
		alert('Email:' + data.d.Email);
	}

	function onError(error) {
		alert("An error!");
	}
} 
</script>

You may be also interested to read SharePoint 2016: JSOM is only working in Edit Mode.


Get Current User Login Name Using REST API

You can use “data.d.LoginName” to get the current login name using REST API.

This will return the current login name with the below format:

Get Current login Name using javascript in SharePoint
i:0#.w|Domain\username

To get only the “domain\username” format use the below code

data.d.LoginName.substring(data.d.LoginName.indexOf('|') + 1);
Get SharePoint user Login Name Using JSOM

You may be also interested to read This web browser either does not support JavaScript or scripts are being blocked.

Get Current User Display Name Using REST API

You can use “data.d.Title” to get the current user display name using REST API.

Get Current user display Name using javascript in SharePoint

Get Current User Email Using REST API

You can use “data.d.Email” to get the current user email using REST API.

Get Current user email using javascript in SharePoint

You may be also interested to read SharePoint 2016: Get Current User Using JavaScript.


Applies To
  • SharePoint 2016.
  • SharePoint 2013.
Conclusion

In this post, I have explained how to use REST API to get the SharePoint Current User details:

  • Login Name.
  • Display Name.
  • Email.
See Also

2 thoughts on “SharePoint 2016: Get Current User Using REST API”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top