Different ways to Retrieve logged in user details in SharePoint 2013/2016/Online

This SharePoint tutorial, we will discuss various ways to retrieve logged in user details in SharePoint 2016/2013 or SharePoint Online.

There are different ways we can retrieve current logged in user details like UserID, Login Name, Login ID, Logged in user display name, etc. Lots of time in SharePoint 2013 or SharePoint we may need to retrieve logged in user details.

Different approaches to retrieve current logged in user details:

  • Retrieve logged in user details using SharePoint Server object model (SSOM)
  • Retrieve logged in user details using client side object model (CSOM)
  • Retrieve logged in user details using REST API
  • Retrieve logged in user details using JavaScript object model (JSOM)
  • Retrieve logged in user details using PowerShell

Let’s take a look at each approach along with the code one by one.

Get logged in user details using SharePoint Server object model (SSOM)

We can easily retrieve logged in user details using the SharePoint Server object model (SSOM). Remember the below code you have to run in the SharePoint server because it requires Micorosft.SharePoint.dll

Code:

SPSite site = new SPSite("http://Site URL");
SPWeb web = site.OpenWeb();
SPUser user = web.CurrentUser;
Console.Write("SSOM\nUser Information\nUser ID : " + user.ID + "\nUser Login Name : " + user.LoginName + "\nUser Title: " + user.Name);
Console.ReadLine();
Retrieve logged in user details using SharePoint Server object model

Get logged in user details using Client Side Object Model (CSOM)

Similarly, we can retrieve logged in user details by using the Client-Side Object Model (CSOM). To run client-side code we need the below two dlls:

  • Microsoft.SharePoint.Client.dll
  • Microsoft.SharePoint.Client.Runtime.dll

Code:

using (ClientContext context = new ClientContext("http://Site URL"))
{
NetworkCredential myNetCred = new NetworkCredential("akashkumhar", "admin@123");
context.Credentials = myNetCred;
Web web = context.Web;
context.Load(web);
User user = context.Web.CurrentUser;
context.Load(user);
context.ExecuteQuery();
Console.Write("CSOM\nUser Information\nUser ID : " + user.Id + "\nUser Login Name : " + user.LoginName + "\nUser Title: " + user.Title);
Console.ReadLine();
}

Output:

Retrieve logged in user details using client side object model

Get logged in user details using REST API

First, we will see how to check the server response on the REST API request by hitting a Simple EndPoint URL on the browser.

This EndPoint URL provides all the information about the current logged in user. The URL is constructed with the use of CurrentUser as the EndPoint, which returns the User ID, Login Name, Title, etc.

EndPoint URL:

<Your Site>_api/web/CurrentUser

Example: http://win-eqalhem27jl:7575/sites/one/_api/Web/CurrentUser

Output:

Retrieve logged in user details using REST API SharePoint Online

We can also get the information about the current logged in user by fetching the current logged in user details from the User Information List.

For this approach, we need the ID of the current logged in user.

The EndPoint URL for this can be constructed with SiteUserInfoList/items(Curent User ID) as the EndPoint.
EndPoint URL:

<your site>_api/web/SiteUserInfoList/items(<your user ID>)?

Example: http://win-eqalhem27jl:7575/sites/one/_api/Web/SiteUserInfoList/Items(8)?$select=ID,Name,Title

Output:

Retrieve logged in user details using REST API sharepoint

Practical Usage of the EndPoint URLs:

Code:

<script src="http://win-eqalhem27jl:7575/sites/one/SiteAssets/1.11.2.jquery.js"></script>
<script type="text/javascript">
$(function () {
fetchUserInfo();
});

function fetchUserInfo()
{
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUserInfoList/Items(8)?$select=ID,Name,Title",
method: "GET",
headers:
{
"Accept": "application/json;odata=verbose",
},
cache: false,
async: false,
success: function(data)
{
var userID = data.d.ID;
var userLoginName = data.d.Name;
var userName = data.d.Title;
alert("User Details: ID > " + userID + ", Name > " + userName + ", LoginName > " + userLoginName)
},
error: function(data)
{
alert(data.responseJSON.error);
}
});
}
</script>

Output:

Retrieve logged in user details using REST API

Get logged in user details using JavaScript Object Model (JSOM)

By using the JavaScript object model we will be able to retrieve current logged in user’s details.

Code:

<script src="http://win-eqalhem27jl:7575/sites/one/SiteAssets/1.11.2.jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var currentUser;
if (SP.ClientContext != null) {
SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.js');
}
else {
SP.SOD.executeFunc('sp.js', null, getCurrentUser);
}
});

function getCurrentUser() {
context = new SP.ClientContext.get_current();
web = context.get_web();
currentUser = web.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(onSuccessMethod, onRequestFail);
}

function onSuccessMethod(sender, args) {
var userID = currentUser.get_id();
var userLoginName = currentUser.get_loginName();
var userName = currentUser.get_title();
currentUserAccount = userLoginName.substring(userLoginName.indexOf("|") + 1);
}

function onRequestFail(sender, args) {
alert('request failed' + args.get_message());
}

</script>

Output:

Retrieve logged in user details using JavaScript

Get logged in user details using PowerShell

We can retrieve current logged in user details using PowerShell in SharePoint 2013. We can run the below PowerShell script using Windows PowerShell ISE or by using visual studio code.

Code:

Add-PSSnapin Microsoft.Sharepoint.Powershell
$site=Get-SPSite "http://win-eqalhem27jl:7575/sites/one/"
$web=$site.RootWeb
$strLoginName=$web.CurrentUser.LoginName
$strID=$web.CurrentUser.ID
$strName=$web.CurrentUser.Name
Write-Host " Current User Id : " $strID -ForegroundColor green;
Write-Host " Current User Login Name : " $strLoginName -ForegroundColor green;
Write-Host " Current User Name : " $strName -ForegroundColor green

Output:

Retrieve logged in user details using PowerShell

You may like following SharePoint tutorials:

Here in this SharePoint 2013 tutorial, we have discussed different ways to get the current logged in user details on the SharePoint Site. This can also be achieved using web services _vti_bin/UserProfileService.asmx

This SharePoint tutorial we learned how to retrieve logged in user details using Rest API, CSOM, SSOM, PowerShell, etc.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Akash Kumhar

I am Akash Kumhar a SharePoint Consultant.

  • […] ways to Retrieve logged in user details in SharePoint 2013 or SharePoint […]

  • >