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

SharePoint 2013 get logged in user display name Rest API
InfoPath alternatives for form designing SharePoint

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 .

Read some SharePoint tutorials

SharePoint 2013 get logged in user display name
SharePoint 2013 get logged in user display name

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.

SharePoint online get logged in user display name
SharePoint online get logged in user display name



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

SharePoint 2013 get logged in user display name Rest API
SharePoint 2013 get logged in user display name Rest API

Similar SharePoint 2013 Tutorials


About Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

View all posts by Sagar Pardeshi →

Leave a Reply