Different ways to Retrieve logged in user details in SharePoint 2013 or SharePoint 2016

sharepoint 2013 get current user javascript

Here we will discuss various ways to retrieve logged in user details in SharePoint 2016 or SharePoint 2013. 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:
1. Retrieve logged in user details using SharePoint Server object model (SSOM)
2. Retrieve logged in user details using client side object model (CSOM)
3. Retrieve logged in user details using REST API
4. Retrieve logged in user details using JavaScript object model (JSOM)
5. Retrieve logged in user details usingPowerShell

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 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();

Output:

how to get logged in user details in sharepoint
how to get logged in user details in sharepoint

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

Similarly we can retrieve logged in user details by using 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:

sharepoint 2013 rest api get current user
sharepoint 2013 rest api get current user

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.



Read: How to make synchronous Rest API call in SharePoint online using jQuery?

EndPoint URL:
<Your Site>_api/web/CurrentUser

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

Output:

sharepoint 2013 get current user javascript
sharepoint 2013 get current user javascript

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:

sharepoint 2013 javascript get current user display name
sharepoint 2013 javascript get current user display name

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”,

meathod: “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:

get current logged in user in sharepoint using jquery
get current logged in user in sharepoint using jquery

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

By using 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:

get current logged in user in sharepoint csom
get current logged in user in sharepoint csom

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:

sharepoint 2013 get current user powershell
sharepoint 2013 get current user powershell

Summary:
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

Hope this article was helpful.


You May Also like the Following SharePoint Online Tutorials:

About Akash Kumhar

I am Akash Kumhar a SharePoint Consultant.

View all posts by Akash Kumhar →

Leave a Reply