SharePoint redirect to another page

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="http://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="http://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.

See also  The following feature couldn't be installed .net framework 3.5

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 users based on browser language in SharePoint 2010/2013/2016/Online.

Redirect to new URL from SharePoint site

In 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:

See also  Power Automate Copy Files from OneDrive to SharePoint

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 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, whenever a user opens the SharePoint site, it will redirect to the specified URL.

In the same way, we can redirect to a new URL from the 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 operations 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.

Redirect to a different page after submitting an item to the SharePoint list

Now, let us see, how to redirect to a different page after the user submits an item to a SharePoint list.

See also  How to find product number of hp laptop using command prompt

By default when the user submits an item to a SharePoint 2013 list, it redirects to the AllItems.aspx page (AllItems view page).

But my requirement is after the user submits the item it should go to a Thank you page (custom site page created to display a thank you message).

By using this tricky approach we can easily redirect to a different page after adding or submitting an item to a SharePoint 2013/2016 or SharePoint Online list.

This might not be the best approach and one of the solutions you can try out. Here are a few things involved in this process:

Step-1:
Make sure you have your custom page is ready where you want your user to navigate after adding the item. I have just created one site page and the URL is :

https://SiteURL/SitePages/ThankYou.aspx

Step-2:
Then open your SharePoint 2013 list and then click on a +new item (to add an item to the list). Then copy the URL from the browser. It will be something like the below:

https://SiteURL/Lists/MyTestingList/NewForm.aspx?Source=https%3A%2F%2FSiteURL%2FLists%2FMyTestingList%2FAllItems%2Easpx&RootFolder=

Here notice the parameter “Source=” which has one URL assigned to it. Basically, that is the AllItems.aspx view URL. So when the user Submits an item to the list, then the user usually redirects to that page.

Now remove everything presented after “Source=”. And then put your custom page URL which looks like the below:

https://SiteURL/Lists/MyTestingList/NewForm.aspx?Source=https://SiteURL/SitePages/ThankYou.aspx

Here even you can put any external page like:

https://SiteURL/Lists/MyTestingList/NewForm.aspx?Source=https://www.enjoysharepoint.com

Step-3:
Then remove the +new item link which is presented in the list. You can follow this article to do that.

Step-4:
Now we give an option in the left navigation to add an item to the list.

Open your site, from the Settings icon click on Site Settings. Then in the Site Settings page, click on Navigation under Look and Feel.

Here go to “Structural Navigation: Editing and Sorting” section in the Navigation Settings page.

Here click on the Add Link and then in the Navigation Setting dialog box to give one Title and in the URL put the URL which we have prepared in the Step-2 which will look like below:

Redirect to different page after submitting an item to SharePoint 2013 list
Redirect to different page after submitting an item to SharePoint 2013 list

After this you will be able to see the link in the left navigation like below:

Redirect to a different page after adding new list items in SharePoint
Redirect to a different page after adding new list items in SharePoint

Now when the user clicks this navigation link then it will be redirected to the Add New Item page.

There whenever a user submits an item, he will be redirected to the page you have mentioned in the Source parameter. I hope this will be helpful to implement a redirect to another page after submitting an item to the SharePoint 2013 list.

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
  • Redirect to a different page after submitting an item to SharePoint list
  • >