![]()
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
ย
ย
ย
ย
About Post Author
Discover more from Global SharePoint
Subscribe to get the latest posts sent to your email.