Redirect the specific user to a different page in SharePoint online – 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: Redirect the specific user to a different page in SharePoint online (URL redirect in SharePoint online)
- 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 web part 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 in javascript

<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, that when all users logged into the SharePoint particular site – they will be landed on the as-usual site home page. However, when the “Test1” user logged in to the SharePoint site – that user should be redirected to the “testhomepageforTest1User.aspx” page.
So, first get 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.
URL Redirect in SharePoint Online: 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 “Web part” link.
- Under the “Media and Content” Categories – select the “Script Editor” web part.
- Click on the “EDIT SNIPPET” and then add the above javascript code over here inside the “script” tag.

How to redirect to another page from the mobile browser (How to redirect URL in android)?
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 on 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: Redirect the specific user to a different page in SharePoint online
Summary: Redirect the specific user to a different page in SharePoint online
Thus, in this article – we have learned the below with respect to currently logged-in user information and redirected 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 web part in SharePoint 2013/2016/2019?
- How to redirect another page using javascript in SharePoint?
- How to redirect to another page from the mobile browser?
See Also: SharePoint Online PowerShell tutorial
You may also like the following SharePoint Online PowerShell tutorials:
- [Verified] Get all webparts from pages in a site using PowerShell in SharePoint Online
- Get document library inventory report in SharePoint using PowerShell script
- How to start SharePoint list workflow using PowerShell
- How to hide quick launch menu in SharePoint online using PnP PowerShell
- Edit user Permission is greyed Out SharePoint Online
- Get workflow inventory from SharePoint online using PowerShell CSOM
- Create a modern team site using PnP PowerShell in SharePoint
- In 2 steps convert a classic SharePoint page to modern using PnP
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- Create SharePoint online list using PnP provisioning template
- SharePoint Automation: PowerShell script to get remote server information
- Office 365: Retrieve hub sites and associated sites using PnP Powershell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
- SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM
- [Solved] Fix the “The term ‘Get-SPWeb’ is not recognized as the name of a cmdlet, function” PowerShell error