When customizing SharePoint pages, adding web parts, or working with SharePoint Framework solutions, you might see the error: “Server relative URL must start with SPWeb server relative URL.” This error prevents a page or web part from loading correctly. The root cause is a mismatch between the URL you supplied in your code or configuration and the actual server-relative path of the SharePoint site. This article explains exactly why this error occurs and how to fix it by using the correct URL format.
Key Takeaways: Fixing the Server Relative URL Error
- Server-relative URL format: Must start with the site collection path, for example
/sites/MySite, not a full URL likehttps://contoso.sharepoint.com/sites/MySite. - SPWeb object context: The URL must match the web application’s root path or the site collection root, not a subsite path.
- Using
$webvariable in SharePoint Framework: Always prepend the server-relative URL with the web’s server-relative URL usingthis.context.pageContext.web.serverRelativeUrl.
Why the Server Relative URL Error Occurs
SharePoint uses server-relative URLs to reference resources like pages, lists, and files within a site collection. A server-relative URL omits the protocol and domain, starting with the managed path, for example /sites/MySite or /teams/MyTeamSite. The error “Server relative URL must start with SPWeb server relative URL” fires when your supplied URL does not begin with the same path as the current SharePoint web (SPWeb) object.
The SPWeb object represents a specific SharePoint site, which could be the root site of a site collection or a subsite. Each SPWeb has a ServerRelativeUrl property. For example, a site at https://contoso.sharepoint.com/sites/Projects/Subsite has a server-relative URL of /sites/Projects/Subsite. If you try to set a page URL to /sites/Projects/Pages/Home.aspx, the URL is valid for the site collection root but not for the subsite. The correct URL for the subsite would be /sites/Projects/Subsite/Pages/Home.aspx.
Common Scenarios That Trigger the Error
The error appears in several situations:
- SharePoint Framework (SPFx) web parts: When you hard-code a server-relative URL in a web part property instead of using the page context.
- Custom page layouts or master pages: When a link or resource reference uses a URL that does not start with the current site’s server-relative path.
- PowerShell scripts or CSOM code: When you pass a server-relative URL to a method that expects the URL to be relative to the current SPWeb.
- SharePoint Designer workflows: When a workflow action references a list or library using an absolute or incorrectly formed relative URL.
The core problem is always the same: the URL you provided does not start with the same path as the site you are working in.
Steps to Fix the Server Relative URL Error
Method 1: Correct the URL in SharePoint Framework Web Parts
- Identify the current web’s server-relative URL
In your SPFx web part code, usethis.context.pageContext.web.serverRelativeUrlto get the correct base path. For a site at/sites/MySite, this property returns/sites/MySite. - Prepend the base path to your target URL
Instead of writing/sites/MySite/Lists/Tasks, build the URL dynamically:this.context.pageContext.web.serverRelativeUrl + '/Lists/Tasks'. This ensures the final URL starts with the correct SPWeb path. - Test the web part in a different site
Deploy the web part to a subsite. Verify that the URL updates automatically to include the subsite path. If the web part still uses a hard-coded URL, the error will reappear.
Method 2: Fix URLs in CSOM or PowerShell Code
- Get the SPWeb object
In CSOM, useclientContext.Webto obtain the current web. In PowerShell, useGet-SPWebwith the site URL. - Read the
ServerRelativeUrlproperty
For the web object, access$web.ServerRelativeUrl(PowerShell) orweb.ServerRelativeUrl(CSOM). Store this value in a variable. - Construct the full server-relative path
Combine the web’s server-relative URL with the relative path of the resource. Example:$web.ServerRelativeUrl + '/Lists/MyList'. Do not include the absolute domain. - Validate the final URL
Check that the combined URL starts with the web’s server-relative URL. If it does not, adjust the resource path to be relative to the web root.
Method 3: Correct URLs in Page Layouts and Master Pages
- Use the
~sitecollectiontoken
In SharePoint Designer or HTML, replace hard-coded paths with~sitecollectionor~site. The token~sitecollectionresolves to the site collection’s server-relative URL. The token~siteresolves to the current site’s server-relative URL. - Rewrite absolute URLs
If a link uses an absolute URL likehttps://contoso.sharepoint.com/sites/MySite/Pages/Home.aspx, change it to~site/Pages/Home.aspx. This ensures the link works on any site within the collection. - Test with a subsite
Navigate to a subsite and verify that all links and resources load without the error. If the error persists, check that the token resolved to the correct path.
If the Error Persists After the Main Fix
“Server Relative URL Must Start With SPWeb Server Relative URL” in SharePoint Designer
When editing a page in SharePoint Designer, you might see this error after changing the URL of a linked resource. The cause is often a hard-coded URL in a Data View web part or a hyperlink. To fix it, open the page in advanced mode, find the resource URL, and replace it with a token or a server-relative URL that starts with the current site path. Use ~site for resources that should follow the current site.
Error When Adding a Web Part Programmatically
If you add a web part via code and provide a server-relative URL for a property like ContentLink, ensure the URL is relative to the web where the web part is added. For example, if you add a web part to a subsite at /sites/MySite/Subsite, the URL must start with /sites/MySite/Subsite. A common mistake is to use the site collection root path instead. Always obtain the current web’s server-relative URL dynamically.
SharePoint Framework Property Pane URL Validation
In SPFx, if you have a property pane field that accepts a server-relative URL, the user might enter an incorrect value. Add validation to your web part that checks whether the entered URL starts with this.context.pageContext.web.serverRelativeUrl. If it does not, show an error message and prevent the web part from rendering. This catches the issue before the error occurs.
Server-Relative URL vs Absolute URL vs Site-Relative URL
| Item | Server-Relative URL | Absolute URL | Site-Relative URL |
|---|---|---|---|
| Format | /sites/MySite/Pages/Home.aspx |
https://contoso.sharepoint.com/sites/MySite/Pages/Home.aspx |
/Pages/Home.aspx |
| Includes domain | No | Yes | No |
| Includes managed path | Yes | Yes | No |
| Works across site collections | Yes | Yes | No |
| Works across subsites | Yes | Yes | No |
| Common use case | APIs, SPFx, CSOM | External links, emails | Same-site navigation, images |
The error only applies to server-relative URLs. If you use an absolute URL, SharePoint typically accepts it but may still throw the error if the code explicitly expects a server-relative URL. The safest approach is to always use server-relative URLs built from the current web’s context.
Now you can identify and fix the “Server relative URL must start with SPWeb server relative URL” error. Always use dynamic methods to obtain the current web’s server-relative URL, whether in SPFx, PowerShell, or page layouts. For complex solutions, test on both root sites and subsites to catch path mismatches early. A reliable practice is to store the web’s server-relative URL in a variable at the start of your code and prepend it to every resource path.