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
- SSOM
- CSOM
- REST API
- JSOM
- 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
- SPSite site = new SPSite("http://win-eqalhem27jl:7575/sites/one/");
- 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
Client Side Object Model (CSOM)
Code
- using(ClientContext context = new ClientContext("http://win-eqalhem27jl:7575/sites/one/"))
- {
- 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
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
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
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
JSOM
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
PowerShell
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
SummaryIn 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.