Redirect the specific user to a different page in SharePoint online - Get current logged in user information in SharePoint using Javascript and redirect to another page

Instantly how to redirect the specific user to a different page in SharePoint online in 2 steps?

No comments

 119,042 total views,  1 views today

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

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, 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 add script editor webpart in SharePoint 2013/2016/2019
How to add script editor web part in SharePoint 2013/2016/2019?

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:

 

 

 

 

About Post Author

Do you have a better solution or question on this topic? Please leave a comment