SharePoint Online redirect to another page programmatically

In this SharePoint tutorial, we will discuss SharePoint Online redirect to another page programmatically.

Here we will discuss various ways to redirect a user to another page in SharePoint 2013/2016 or SharePoint Online using JavaScript or jQuery.

SharePoint 2013 redirect page to another page is a very common requirement where users will be redirected to another page in SharePoint 2013/2016 or SharePoint Online.

The destination page can be a page that exists in the SharePoint site or it can be an external page.

SharePoint Online redirect to another page programmatically

Below are a few options you can use to redirect page to another page in SharePoint 2013/2016 or SharePoint Online.

We can add a script to redirect a page to another page in SharePoint. In various ways, you can add a script to a SharePoint page to redirect.

  • Script Editor / Content Editor web page: Edit the web part page and add a script editor web part or content editor web part to add the javascript
  • Directly on the Page: You can edit the page using SharePoint designer 2013 and then you can add the JavaScript or jQuery code to redirect the user to another page in SharePoint.
SharePoint Online redirect to another page programmatically
sharepoint 2013 redirect page to another page

Redirect user to another page in SharePoint 2013

You can write the below script in the source page in SharePoint to Redirect user to another page.

<script language="javascript" src="https://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
window.location = "https://www.enjoysharepoint.com";
});
</script>

Or you can try below option:

<script language="javascript" src="https://code.jquery.com/jquery-1.6.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
window.location.href = "https://www.enjoysharepoint.com";
});
</script>

SharePoint 2013 redirect page to another page

You can also add the below script to redirect page to another page in SharePoint 2013/2016 or SharePoint Online.

<script type="text/javascript">
function NavigatetoEnjoySharePoint() {
setTimeout("location.href='https://www.enjoysharepoint.com'", 5000);
}
_spBodyOnLoadFunctionNames.push("NavigatetoEnjoySharePoint");
</script>

SharePoint online redirect page

You can also write the below code for SharePoint online redirect page.

<script type="text/javascript">
// <![CDATA[
alert("You will be redirected to enjoysharepoint.com now.");
// ]]>
</script>
<meta http-equiv="refresh" content="0;url=https://www.enjoysharepoint.com/">

Once you save the page and refresh it will redirect. If next time you want to modify, then sometimes you can not because it will redirect very fast. So you can try to access the urls like this:

https://SiteURL/pages/home.aspx?contents=1

Then you can select the particular web part and then you can close or delete the web part. After this, the redirection will stop.

This will be helpful more if you have put the code in script or content editor web part. If you have put on the page using a designer then you can change the code using the designer.

I hope this will be helpful for SharePoint 2013 redirect page to another page. You can easily redirect a user from one page to another page in SharePoint Online or SharePoint 2013/2016.

Redirect User to Success or Error page in SharePoint 2013/2010

Now, we will see how to redirect a user to a Success page or Error page in SharePoint 2016/2013/2010 by using the SPUtility class.

Microsoft provides SPUtility class which we can use to send the user to a success or error page. SPUtility class will be available in Microsoft.SharePoint.Utility namespace.

Redirect user to Success Page in SharePoint 2013/2010

Below is how you can SPUtility.TransferToSuccessPage() method to redirect a user to a success page in SharePoint 2016/2013/2010.

SPUtility.TransferToSuccessPage("Operation was completed", @"default.aspx", "", "");

You can pass any custom message you want. See the figure below:

Redirect User to Success or Error page in SharePoint
Redirect User to Success or Error page in SharePoint

Redirect user to Error Page in SharePoint 2013/2010

We can use SPUtility.TransferToErrorPage method to redirect a user to an error page in SharePoint 2013/2010.

Exception ex = new Exception("An unexpected error has occurred.");
SPUtility.TransferToErrorPage(ex.Message);

See the figure below for reference.

Redirect User to Success or Error page in SharePoint 2013
Redirect User to Success or Error page in SharePoint 2013

Here, we saw how to redirect a user to a success or error page in SharePoint 2013/2010 using SPUtility class.

SharePoint redirect to access denied page

This SharePoint 2010 tutorial explains, how to redirect to access denied page in SharePoint 2010/2013. There are different ways to send the user to Access denied page in SharePoint 2010/2013.

There will be certain situations where you can send the user to the Access denied page. Below are two ways to send the user to the Access denied page.

1st Approach:

You can send user to Access denied page by using SPUtility.Redirect method like below:

SPUtility.Redirect(SPUtility.AccessDeniedPage, SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Context);

2nd Approach:

You can send user to Access denied page by using SPUtility.HandleAccessDenied method like below:

SecurityException ex = new SecurityException();
SPUtility.HandleAccessDenied(ex);

The below figure is the access denied page in SharePoint 2010.

sharepoint redirect to access denied page
sharepoint redirect to access denied page

This explains SharePoint redirect to access denied page.

Redirect user based on browser language in SharePoint

In this SharePoint tutorial, we will discuss how to redirect the user to different sites based on browser language in SharePoint using JavaScript.

The same JavaScript code we can use to redirect the user to different sites based on browser language in SharePoint 2010/2013/2016 or SharePoint Online.

Redirect user based on browser language in SharePoint

Here we will use JavaScript _spPageContextInfo variable to know about browser language as well as to redirect to a different site in SharePoint.

Below is the code to retrieve browser language in JavaScript.

var currentSetLanguage = _spPageContextInfo.currentLanguage;
var curentSetWebLanaguage =_spPageContextInfo.webLanguage;

Here in both cases it will return the language code, for example, it will return 1033 for English.

You can put the below code inside a script editor or content editor web part in SharePoint.

<script>
$(function(){
var languageSett = _spPageContextInfo.currentLanguage;

if(languageSett !== 1033){
document.location.href = “/sites/India/default.aspx”;
}
else if(languageSett == 1031){
document.location.href = “/sites/germany/default.aspx”;
}
else if(languageSett == 1049){
document.location.href = “/sites/Russian/default.aspx”;
}
});
</script>

This is how we can redirect user based on browser language in SharePoint 2010/2013/2016/Online.

Redirect to new URL from SharePoint site

This SharePoint tutorial we will discuss how to redirect to a different URL from a SharePoint 2010 site. This will be achieved by using a content editor web part. Learn how to redirect the SharePoint 2010/2013/Online site to a new URL.

Before moving forward follow the below steps to upload the script to a document library.

Open a notepad and paste the below code and save it.

<script "text/javascript">//<![CDATA[alert("You will be redirected automatically in 10
seconds!!!");//]]></script><meta http-equiv="refresh" content="10";url="https://www.enjoysharepoint.com">

In the next step upload the text file to a SharePoint document library and copy the path of the file.

I have uploaded this to the Shared Documents document library and the path is given below for me.

http://bsahoo3:2500/sites/TestSiteCollection/Shared%20Documents/myScript.txt

Now the time has come to add a content editor web part to the home page. follow the below steps:

Next, edit the web part page and click on Add web part. From the Ribbon click on Insert -> Add web part.

From the list of web parts, Select Media and Content category and Select Content Editor and click on Add as shown in the figure below.

sharepoint redirect to new url
sharepoint redirect to new url

After that edit the web part and give the link of the notepad file that we saved above in the Content Link of the web part properties window. Then click on OK as shown in the figure below.

redirect sharepoint 2010 site to new url
redirect sharepoint 2010 site to new url

Now Save and close the page. Now, when ever an user open the SharePoint site, it will redirect to the specified URL.

In the same way, we can redirect to new URL from SharePoint site like to SharePoint Online site, or SharePoint 2013/2016 site.

Create Custom Save and Redirect Button On a SharePoint Form

Now, we will see how to Create Custom Save and Redirect Button on a SharePoint Form using SharePoint Designer, since the best way to work on an ASPX page would be through SharePoint Designer 2013.

Scenario:

Recently, I came across a scenario, where I had to create a custom save button on a SharePoint Form and on the click of the button.

I had to do some operation before triggering validation and ultimately save the item and redirect, so I would like to share my knowledge on it and the way of implementation.

Implementation:

Here, we will be learning the following.

  • How to create a custom save button on .ASPX page?
  • How to redirect on button click in .ASPX page?
  • How to call a custom function on button click in .ASPX page?
  • How can we pass a parameter in a redirection URL?
Create Custom Save and Redirect Button On a SharePoint Form

Below is the step by step implementation of the same.

First, create an HTML input tag with type button on .ASPX page. Let’s say I have done it on EditForm.aspx.

<input style="margin: 0″ id="customSave" type="button" value="custom Save" name="btnSave" onclick="if (!ExecuteThisFunctionBeforeSavingAndRedirecting()) return false; { ddwrt: GenFireServerEvent(‘__commit;__redirect={webAbsoluteUrl/Lists/ListName/NewForm.aspx?isCustomSaved=Yes}’) }" />

It is onclick, where we have to do some operations before saving. So write and check if the function returns true to proceed and save, as shown below.

onclick="if (!ExecuteThisFunctionBeforeSavingAndRedirecting()) return false;"

If the function executes correctly and returns true, the form is saved, using the command given below.

{ddwrt:GenFireServerEvent(‘__commit’)}

Now, for committing and redirecting to another location, let’s say here that we are redirecting to the NewForm.

We write:

{ddwrt:GenFireServerEvent(‘__commit;__redirect={webAbsoluteUrl/Lists/ListName/NewForm.aspx}’)}

After redirecting, when NewForm opens; it will be the same as created on a new item and not the redirected form, so for this, set a parameter in the redirection URL.

__redirect={webAbsoluteUrl/Lists/ListName/NewForm.aspx?isCustomSaved=Yes}

Second, the operation which you need to perform has to be coded somewhere. I have included that particular function in the JavaScript file and gave the reference of the NewForm.js in the ScriptEditor Webpart inserted on EditForm.aspx.

NewForm.js:

function ExecuteThisFunctionBeforeSavingAndRedirecting() {
//Do Something
if (!PreSaveItem()) return false;
else return true;
}

function PreSaveItem() {
//Do Validation Code
}

Here, once the operation is done, call for the PreSaveItem(); function where the code for validation is present. If the PreSaveItem returns true, the forms saves successfully and redirects, else the form doesn’t save.

You may like the following SharePoint tutorials:

In this tutorial we learned:

  • SharePoint Online redirect to another page programmatically
  • SharePoint online redirect page
  • Redirect User to Success or Error page in SharePoint 2013/2010
  • SharePoint redirect to access denied page
  • Redirect user based on browser language in SharePoint
  • Redirect to new URL from SharePoint site
  • Create Custom Save and Redirect Button On a SharePoint Form
Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SPGuides.com

>