Get current logged in user information in SharePoint using Javascript and redirect to another page

[Verified] How to redirect the specific user to a different page in SharePoint online?

No comments

 1,154 total views,  22 views today

Sometimes, based on the user we need to redirect a user to a different page. Here, in this blog, I will share how to redirect the specific user to a different page in SharePoint online using the Javascript CSOM code.

Key-Highlights:

  • How to get current logged user information using javascript?
  • How to redirect the specific user to a different page in SharePoint online/2013/2016/2019 using the javascript CSOM code?
  • How to add script editor webpart in SharePoint 2013/2016/2019/Online?
  • How to redirect another page using javascript in SharePoint?

Redirect the user to another page in SharePoint 2013/2016/2019/ and SharePoint Online using Javascript CSOM code:

Get current logged in user information in SharePoint using Javascript and redirect to another page
Get current logged in user information in SharePoint using Javascript and redirect to another page

<script type=”text/javascript”>


ExecuteOrDelayUntilScriptLoaded(init,'sp.js');
var currentLoggedInUser;
function init()
{
this.clientContext = new SP.ClientContext.get_current();
this.objWeb = clientContext.get_web();
currentLoggedInUser = this.objWeb.get_currentUser();
this.clientContext.load(currentLoggedInUser);
this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded()
{
document.getElementById('currentUserLoginName').innerHTML = currentLoggedInUser.get_loginName();
document.getElementById('currentUserEmailID').innerHTML = currentLoggedInUser.get_email();
document.getElementById('currentUserTitle').innerHTML = currentLoggedInUser.get_title();
document.getElementById('currentUserId').innerHTML = currentLoggedInUser.get_id();

/*Once current logged in user is found with the Test1@test.com - this will be redirected to a different page.*/

If(currentLoggedInUser.get_email() =="Test1@test.com")

{
var url= "https://yoursite/sites/sitepages/your_site_page_for_the _dedicated user.aspx";
window.location = url;

}

}

function onQueryFailed(sender, args)
{
alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}

</script>

 
<div>Current Logged-In User Information: 
<span id="currentUserLoginName"></span></br>
<span id="currentUserEmailID"></span></br>
<span id="currentUserTitle"></span></br> 
<span id="currentUserId"></span></br> 
</div>

Explanation about the “Redirect the user to another page in SharePoint 2013/2016/2019/ and SharePoint Online using Javascript CSOM code”:

In the above code, I have a user say named “Test1” and created a “testhomepageforTest1User.aspx” site page. The scenario is, when all users logged into the SharePoint particular site – they will be landed on the as usual site home page. However, when “Test1” user logged in to the SharePoint site – that user should be redirected to the “testhomepageforTest1User.aspx”  page.

So, first getting the current logged in user email in the below line:


currentLoggedInUser.get_email()

Then, checking whether currentLoggedInUser.get_email() ==”Test1@test.com”, if yes – redirect that user to “testhomepageforTest1User.aspx” page.


/*Once current logged in user is found with the Test1@test.com - this will be redirected to a different page.*/

If(currentLoggedInUser.get_email() =="Test1@test.com")

{
var url= "https://yoursite/sites/sitepages/your_site_page_for_the _dedicated user.aspx";
window.location = url;

}

Note:

  • If you are using SharePoint Online, you can directly use the _spPageContextInfo.userLoginName global variable to get the logged-in user login name.

 

How to use the “Redirect the user to another page in SharePoint 2013/2016/2019/ and SharePoint Online using Javascript CSOM code” on your page?

Well, we have the code ready, now inject the above to the SharePoint page. To do that follow the below steps:

  • Click on the edit page (where you want to insert this code).
  • Click on the Insert button from the ribbon.
  • Click on the “Webpart” link.
  • Under the “Media and Content” Categories – select the “Script Editor” webpart.
  • Click on the “EDIT SNIPPET” then add the above javascript code over here inside the “script” tag.
How to add script editor webpart in SharePoint 2013/2016/2019
How to add script editor webpart in SharePoint 2013/2016/2019?

 

How to redirect to another page from the mobile browser?

Not all mobile devices support javascript and the way handling redirection from the mobile browser is different, we can use the below code to redirect to another page in the mobile device.

Redirecting…
<script language=”javascript”>

if(!window.location.search.substring(1) == "full=true") { // do not redirect if querystring is ?full=true
if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/Blackberry/i) || navigator.userAgent.match(/WebOs/i)) { // detect mobile browser
window.location.replace("http://url-of-your-mobile-page"); // redirect if mobile browser detected
}
}

</script>

Reference:

Summary:

Thus, in this article – we have learned the below with respect to current logged in user information and redirect to another page:

  • How to get current logged user information using javascript?
  • How to redirect the specific user to a different page in SharePoint online/2013/2016/2019 using the javascript CSOM code?
  • How to add script editor webpart in SharePoint 2013/2016/2019?
  • How to redirect another page using javascript in SharePoint?
  • How to redirect to another page from the mobile browser?

 

You may also like the following SharePoint PowerShell tutorials: