Get The Current Logged In User Details In SharePoint 2013 Using SSOM, CSOM, REST API, JSOM And PowerShell

Introduction

In this article we will learn how to fetch the user details of the current logged in user in a SharePoint site. There are multiple approaches by which we can get the logged in details such as User ID, Login Name (Login ID) of the user and the Display Name of the user.

We will look into the following five approaches

  1. SSOM
  2. CSOM
  3. REST API
  4. JSOM
  5. PowerShell

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

Server Side Object Model (SSOM)

In SSOM we get the current logged in user to the SharePoint farm

Code

  1. SPSite site = new SPSite("http://win-eqalhem27jl:7575/sites/one/");  
  2. SPWeb web = site.OpenWeb();  
  3. SPUser user = web.CurrentUser;  
  4. Console.Write("SSOM\nUser Information\nUser ID : " + user.ID + "\nUser Login Name : " + user.LoginName + "\nUser Title: " + user.Name);  
  5. Console.ReadLine(); 

 

Output

Output

Client Side Object Model (CSOM)

Code

  1. using(ClientContext context = new ClientContext("http://win-eqalhem27jl:7575/sites/one/"))  
  2. {  
  3.     NetworkCredential myNetCred = new NetworkCredential("akashkumhar""admin@123");  
  4.     context.Credentials = myNetCred;  
  5.     Web web = context.Web;  
  6.     context.Load(web);  
  7.     User user = context.Web.CurrentUser;  
  8.     context.Load(user);  
  9.     context.ExecuteQuery();  
  10.     Console.Write("CSOM\nUser Information\nUser ID : " + user.Id + "\nUser Login Name : " + user.LoginName + "\nUser Title: " + user.Title);  
  11.     Console.ReadLine();  
  12. }  

 

Output

Output

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

Output

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

Output

Practical Usage of the EndPoint URLs

Code

  1. <script src="http://win-eqalhem27jl:7575/sites/one/SiteAssets/1.11.2.jquery.js"></script>  
  2. <script type="text/javascript">  
  3.     $(function() {  
  4.         fetchUserInfo();  
  5.     });  
  6.   
  7.     function fetchUserInfo() {  
  8.         $.ajax({  
  9.             url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/SiteUserInfoList/Items(8)?$select=ID,Name,Title",  
  10.             meathod: "GET",  
  11.             headers: {  
  12.                 "Accept""application/json;odata=verbose",  
  13.             },  
  14.             cache: false,  
  15.             async: false,  
  16.             success: function(data) {  
  17.                 var userID = data.d.ID;  
  18.                 var userLoginName = data.d.Name;  
  19.                 var userName = data.d.Title;  
  20.                 alert("User Details: ID > " + userID + ", Name > " + userName + ", LoginName > " + userLoginName)  
  21.             },  
  22.             error: function(data) {  
  23.                 alert(data.responseJSON.error);  
  24.             }  
  25.         });  
  26.     }  
  27. </script>  

 

Output

Output

JSOM

Code

  1. <script src="http://win-eqalhem27jl:7575/sites/one/SiteAssets/1.11.2.jquery.js"></script>  
  2. <script type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var currentUser;  
  5.         if (SP.ClientContext != null) {  
  6.             SP.SOD.executeOrDelayUntilScriptLoaded(getCurrentUser, 'SP.js');  
  7.         } else {  
  8.             SP.SOD.executeFunc('sp.js'null, getCurrentUser);  
  9.         }  
  10.     });  
  11.   
  12.     function getCurrentUser() {  
  13.         context = new SP.ClientContext.get_current();  
  14.         web = context.get_web();  
  15.         currentUser = web.get_currentUser();  
  16.         context.load(currentUser);  
  17.         context.executeQueryAsync(onSuccessMethod, onRequestFail);  
  18.     }  
  19.   
  20.     function onSuccessMethod(sender, args) {  
  21.         var userID = currentUser.get_id();  
  22.         var userLoginName = currentUser.get_loginName();  
  23.         var userName = currentUser.get_title();  
  24.         currentUserAccount = userLoginName.substring(userLoginName.indexOf("|") + 1);  
  25.     }  
  26.   
  27.     function onRequestFail(sender, args) {  
  28.         alert('request failed' + args.get_message());  
  29.     }  
  30. </script> 

Output

Output

PowerShell

Code

  1. Add - PSSnapin Microsoft.Sharepoint.Powershell $site = Get - SPSite "http://win-eqalhem27jl:7575/sites/one/"  
  2. $web = $site.RootWeb  
  3. $strLoginName = $web.CurrentUser.LoginName$strID = $web.CurrentUser.ID$strName = $web.CurrentUser.Name  
  4. Write - Host " Current User Id : "  
  5. $strID - ForegroundColor green;  
  6. Write - Host " Current User Login Name : "  
  7. $strLoginName - ForegroundColor green;  
  8. Write - Host " Current User Name : "  
  9. $strName - ForegroundColor green  
Output

Output

Summary

In this article we had a look at the 5 different ways to get the current logged in user details on a SharePoint Site. This can also be achieved using web services _vti_bin/UserProfileService.asmx. I hope this article was helpful.