SharePoint Online: How to Implement Custom 404 Page Not Found Error Page?

Requirement: Implement a custom page not found error page in SharePoint Online.

SharePoint Online: How to Add custom 404 page not found error page

SharePoint Online comes with a default “Page not found” basic error page in the “Pages” library in publishing Sites (https://crescent.sharepoint.com/Pages/PageNotFoundError.aspx), which is shown automatically when a user tries to access a page or file that doesn’t exist. But it can be customized to meet specific business requirements. In this article, we will explore how to implement a custom 404 Page Not Found error page in SharePoint Online. This error page works for the entire site collection.

sharepoint online custom page not found

You can further customize this page, located at “/Pages/PageNotFoundError.aspx” to add some descriptive text, images, and branding. In some cases, You may need to create your own 404 page, and here are the steps:

Step 1: Add New Page Not Found Error Page in SharePoint Online: 

We can create our custom page and set it as a page not found error page for any SharePoint Online site collection. Here are the steps to create a custom 404 page not found error page:

  1. Go to Site Contents >> Pick Pages Library and Create New >> Error Page.
  2. Customize the error page with your styles, and images, and save.

Step 2: Set Default 404 Page Not Found Error page:

Once the custom error page is ready, the next step is to configure the page as a default 404 page not found error page. This can be done either through PowerShell or using SharePoint Designer.

Make sure you have enabled the custom script prior to setting Property bag values!

Option 1: PowerShell to Set Custom Error page in SharePoint Online:
To set a custom page not found error page, we need to update the “vti_filenotfoundpage” key in the property bag of the root site object. Here is the PowerShell script to set a custom error page for a site collection.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$CustomPageNotFoundURL = "/sites/marketing/Pages/404.aspx"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

    #Get the Root Web and its Property Bag
    $Web = $Ctx.Web
    $Ctx.Load($Web)
    $Ctx.Load($Web.AllProperties)
    $Ctx.ExecuteQuery()
    
    #Update the Key value in Property bag
    $Web.AllProperties["vti_filenotfoundpage"] = $CustomPageNotFoundURL
    $Web.Update()
    $Ctx.ExecuteQuery()
  
    Write-Host -f Green "Custom 404 Page Not Found Error page has been Set!"
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

You can also set the 404 Error page using PnP PowerShell as:

Set-PnPPropertyBagValue -Key "vti_filenotfoundpage" -Value "/sites/Your-site-url/SitePages/Page-not-found.aspx"

Option 2: Using SharePoint Designer to set Custom Page not found error page:
You can also use SharePoint designer to set the custom page not found error page: Here is how:

  1. Open your SharePoint Online site collection in SharePoint Designer 2013
  2. Select the Site object on the left side navigation, Click on the Site Options button in the ribbon 
  3. Assign the “vti_filenotfoundpage” property of the site to a Custom Error Page URL that you created. (Add a new entry, if it doesn’t exist)
    sharepoint online custom 404 page
  4. Click on the “OK” button to commit your changes.

Can I implement a custom page not found error page without activating Publishing Feature?

How about non-publishing sites? How to add custom 404 error pages in Team sites? Here is how:

  • Navigate to your SharePoint Online site collection, Pick any document library (E.g., “Site Assets”)
  • Enable Content type by: Click on Settings >> Library Settings >> Advanced Settings >> Set “Yes” for the “Allow management of content types” option and click on “OK” at the bottom of the page
  • Now, on the Library Settings page, under “Content Types”, Click on “Add from existing site content types” link
  • Add “Basic Page” content type to the Library. Now, Go back to the library >> Click on “New” >> Basic Page >> Give a name to your error page (E.g., 404) and click on “Create”
  • Add some descriptive text and images to the page and save!

Now, You have your custom error page ready. You can use either PowerShell or SharePoint Designer methods described above to set the custom page not found error page for SharePoint Online!

How to Set Custom 404 Error page in Modern SharePoint Online Sites?

The same procedure applies when you want to implement a custom page not found error page on your modern team site or communication sites. The modern SharePoint Online sites give you just “404 NOT FOUND” text when you try to access a page that doesn’t exist. Create a page (You don’t need the publishing feature enabled! Just a site page will do!) and use this PowerShell script to set the page as a default 404 error page.

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ErrorPageURL = "/sites/Marketing/SitePages/404-error.aspx" #Server Relative URL of the Error page

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Set 404 Error Page
Set-PnPPropertyBagValue -Key "vti_filenotfoundpage" -Value $ErrorPageURL

Make sure you have enabled the custom script for your site collection! How to enable custom script in SharePoint Online?

In conclusion, customizing the 404 Page Not Found error page in SharePoint Online is a simple process that can greatly enhance the user experience for your site visitors. By using custom error pages, you can help users navigate back to the main page of your site and reduce frustration when they encounter broken links or missing pages. Whether you’re a SharePoint administrator or a site owner, implementing a custom 404 error page is a quick and effective way to improve your SharePoint Online experience.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “SharePoint Online: How to Implement Custom 404 Page Not Found Error Page?

  • Nice article, saved me a lot of time 🙂

    Reply

Leave a Reply

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