Set 404 Error Page
This PnP PowerShell script configures a custom 404 error page for SharePoint Online sites. A custom 404 error page provides a better user experience when users navigate to non-existent pages or broken links within your SharePoint site.
Purpose
Setting up a custom 404 error page helps improve user experience by:
- Providing helpful navigation options when users encounter broken links
- Maintaining your site's branding and design consistency
- Reducing user frustration with friendly error messaging
- Offering alternative navigation paths or search functionality
- Tracking and analyzing 404 errors for site improvement
Prerequisites
- PnP PowerShell module installed
- Site collection administrator permissions
- Connection to your SharePoint Online site
- Custom 404 error page already created in the Site Pages library
- Custom scripting enabled for the site
Setup Instructions
- Create your 404 page in the Site Pages library - Design and publish a custom page that will serve as your error page
- Turn on custom scripting for the site - This is required to modify the property bag values
- Update the script with your tenant, site and page details and run it!
PowerShell Script
# Set your Client ID (optional - leave empty for interactive authentication)
$ClientId = ""
# Connect to your SharePoint Online site
Connect-PnPOnline -Url "https://tenantName.sharepoint.com/sites/siteName" -Interactive -ClientId $ClientId
# Set the property bag value to configure the custom 404 error page
Set-PnPPropertyBagValue -Key "vti_filenotfoundpage" -Value "/sites/siteName/SitePages/pageName.aspx"
Usage Notes
- Replace placeholder values: Update "tenantName", "siteName", and "pageName" with your actual values
- Client ID: Optional - leave empty for interactive authentication or provide your app registration ID
- Page path: Use the full relative path to your 404 page (e.g., "/sites/mysite/SitePages/404.aspx")
- Test the configuration: Navigate to a non-existent page to verify the custom 404 page displays
- Custom scripting: Ensure custom scripting is enabled on your site before running this script
Best Practices
- Page design: Design your 404 page to match your site's branding and navigation
- Helpful content: Include navigation links, search functionality, and contact information
- Clear messaging: Provide user-friendly messaging explaining what happened
- Analytics tracking: Consider adding tracking to monitor 404 occurrences for site improvement
- Performance: Keep the page lightweight for fast loading times
- Accessibility: Ensure the page meets accessibility standards
Example Values
- Tenant: contoso.sharepoint.com
- Site: /sites/intranet
- Page: SitePages/PageNotFound.aspx
- Full path: "/sites/intranet/SitePages/PageNotFound.aspx"