Page Redirect using CEWP JavaScript in SharePoint

There are some situations, where we need to place re-directs in SharePoint pages. OK.. How to do that?

How to redirect a SharePoint page?

Simple! We can redirect using content editor web part in SharePoint. Just add a content editor web part, and place the below JavaScript code:

<script type="text/javascript">

// <![CDATA[
    alert("This application has been moved to replaced with another version, please update all existing bookmarks.  You will be redirected momentarily.");
// ]]>
</script>

<meta http-equiv="refresh" content="0;url=https://MyNewURL/default.aspx">

This will redirect to another page. If you want to stop the redirect, use “?noredirect=true” in the URL. E.g. “https://yourdomain.sharepoint.com/sites/YourSite/default.aspx?noredirect=true”. You can use SharePoint’s native method also:

SharePoint redirect page in 5 seconds

 <script type="text/javascript">

  function redirect() {
    //sharepoint 2010 redirect page 5 seconds
    setTimeout("location.href='https://www.NewSite.com'", 5000);  
  }

  _spBodyOnLoadFunctionNames.push("redirect");

 </script>

OK. How do I edit this page again? Because It doesn’t me to allow editing, it redirects me on the fly… Well, you have to go to the web part maintenance page (Just append ?contents=1 at the end of the URL in the browser!) and remove this web part, edit again add the web part with changes!

Generally, for performance reasons, it’s a good idea to place redirects at ISA/Publishing server level. Or at least in IIS Level.

SharePoint redirect before page load
The main disadvantage over the above methods is delay! We’ve to wait until the web part loads. What we can do to mitigate the delay is: Take-off all content from default.aspx and place the below code in it (via SPD):

<%@ Page language="C#" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %> 

<script type="text/javascript">

window.location.href = "https://www.sharepoint.crescent.com/sites/salesv2/default.aspx";

</script> 

If you want a entire site to be redirected, consider: Redirect to another site collection in SharePoint

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

8 thoughts on “Page Redirect using CEWP JavaScript in SharePoint

  • ear Team,
    I am not sure if this is the right place but I need some help in the redirection of page.
    Example:
    I have two SharePoint communication site abc.com and xyz.com.
    abc.com is for staff
    xyz.com is for students
    There is one more modern page 123.com
    I need if the staff go to 123.com it redirects to abc.com and if student go to 123.com it should redirect to xyz.com.
    Please help me in this query

    Reply
  • Hi ,

    when item is added to the list that time new page has to open.

    Thanks

    Reply
    • Well, That can be done with adding ‘Source’ Parameter to the URL. E.g. https://sharepoint.crescent.com/sites/Operations/Lists/Tasks/NewForm.aspx?Source=/sites/operations/SitePages/Home.aspx

      Reply
    • Yes,Where we have to give because of list URL Have TO fetch then only its possible to add the source for that right.
      Example:https://sharepoint.crescent.com/sites/Operations/Lists/Tasks/NewForm.aspx(I have opened this URL then adding the item to the list. when we are saving its has to save and navigate to another page that’s my conditon) how we can achieve this one

      Reply
  • Hi,

    i have 2 edit forms in a list and need to switch the forms depending upon login.

    for approvers group it should display one edit form and for remaining people another edit from.

    Please help.

    Thanks

    Reply
  • Hi,
    I want to do a autoredirect in default.aspx in sharpeoint 2010 to on of my applicationpage placed in layout folder.
    I tried your 2nd code.but it didnt worked.
    pleae help.
    thankyou

    Reply
    • Hi there,
      You can use SPUtility.Redirect or HttpResponse.Redirect methods.
      Regards,
      Sal

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *