Redirect SharePoint Site To New URL

You often get requirements to redirect a SharePoint site to a new URL. In this tutorial, I will explain how to redirect a SharePoint page to another page programmatically. 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

Using a small script, you can programmatically redirect a SharePoint page or site to another page. You can add the scripts in a script or content editor web part.

You can write the script below on the source page in the SharePoint site to redirect the user to another page.

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

Or you can try the below JavaScript code:

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

You can also add the below script to redirect the page to another page in SharePoint. In this case, it will be automatically redirected after five milliseconds.

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

You can also write the code below to redirect a page to another SharePoint 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 close or delete it. After this, the redirection will stop.

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

In this way, you can easily redirect a user from one page to another page in SharePoint Online or SharePoint on-premises versions.

Check out Create SharePoint page template

Redirect User to Success or Error page in SharePoint

Let me show you another unique requirement in SharePoint. You can redirect a user to a success or error page in SharePoint by using the SPUtility class.

Note: This will work only with SharePoint on-premises versions, and you can add the code in a Visual Web Part.

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

Redirect User to the Success Page in SharePoint

Below is how you can use SPUtility.TransferToSuccessPage() method to redirect a user to a success page in SharePoint.

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 Error Page in SharePoint

We can use SPUtility.TransferToErrorPage() method to redirect a user to an error page in SharePoint.

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

This is how to redirect a user to a success or error page in SharePoint using the SPUtility class.

Check out Create and Manage Task List in SharePoint

Redirect User to Access Denied Page in SharePoint

Here, I will show you how to redirect to the access denied page in SharePoint. There are different ways to send the user to the Access denied page in SharePoint.

In certain situations, you can send the user to the Access Denied page. Below are two ways to do this.

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 the user to the Access denied page by using SPUtility.HandleAccessDenied method like below:

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

The figure below is the access denied page in SharePoint.

sharepoint redirect to access denied page

By following any of the above approaches, you can redirect a user to an access denied page in SharePoint.

Read How to Include jQuery in SharePoint

Redirect the User Based on Browser Language in SharePoint

I recently got this particular requirement from one of my clients. They wanted us to redirect users to different sites based on browser language in SharePoint. You can do this by using JavaScript.

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

Below is the code to retrieve the 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.

In SharePoint, you can put the code below inside a script editor or content editor web part.

<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.

I hope you learn how to redirect a SharePoint site to a new URL programmatically. We also covered:

  • Redirect User to Success or Error page in SharePoint
    • Redirect User to the Success Page in SharePoint
    • Redirect user to Error Page in SharePoint
  • Redirect User to Access Denied Page in SharePoint
  • Redirect the User Based on Browser Language in SharePoint

You may also like:

  • >

    Build a High-Performance Project Management Site in SharePoint Online

    User registration Power Apps canvas app

    DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

    Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

    Power Platform Tutorial FREE PDF Download

    FREE Power Platform Tutorial PDF

    Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…