Server relative urls must start with SPWeb.ServerRelativeUrl in SharePoint REST API - Fixed

Instantly Solved: Server relative urls must start with SPWeb.ServerRelativeUrl – Microsoft 365

No comments

Loading

In this “Server relative URLs must start with”  blog, we will learn how to fix the error “Server relative URLs must start with SPWeb.ServerRelativeUrl”. Generally, we will get this error when we try to get all files from a given folder in the SharePoint Online document library using the SharePoint REST API.

For example, if we will call the below API, it is supposed to list out all files from the folder “Folder1” in the “Document” library.

https://globalsharepoint2020.sharepoint.com/sites/GSDRnD/_api/web/GetFolderByServerRelativeURL('/Shared Documents/Folder1')/files

However, once we hit this API in the browser, we get the below error:

“This XML file does not appear to have any style information associated with it. The document tree is shown below.
<m:error xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata”&gt;
<m:code>-2147024809, System.ArgumentException</m:code>
<m:message xml:lang=”en-US”>Server relative urls must start with SPWeb.ServerRelativeUrl</m:message>
</m:error>”

Server relative urls must start with SPWeb.ServerRelativeUrl
Server relative urls must start with SPWeb.ServerRelativeUrl

Notes:

  • In the above API URL “Shared%20Documents” is the internal name of the SharePoint default document library “Documents”

Solution: Server relative urls must start with SPWeb.ServerRelativeUrl (REST API Errors)

First, let’s understand the URL structure or syntax of this API.

SharePoint REST API to get all files from the given folder in the document library

The REST API Syntax is:

SPOSiteURL/_api/web/GetFolderByServerRelativeURL('/<DocumentlibraryInternalName>/<FolderName>')/files

Example:

https://globalsharepoint2020.sharepoint.com/sites/GSDRnD/_api/web/GetFolderByServerRelativeURL('/Shared Documents/Folder1')/files

Notes:

  • Here is the issue with the backslash(“/Shared Documents”) before the document library name, just remove the backslash the API will return all the files from the given folder location, lets’s see it below:
Get all files from a specified folder in SharePoint document library using the REST API
Get all files from a specified folder in the SharePoint document library using the REST API

In the above example, we can see once we have removed the backslash from the (“/Shared Documents”), the REST API returns all files from the given folder location.

However, this rule is not applicable to the tenant root site, the API will work with the backslash and without backslash in the tenant root site.

Let’s have a look at the tenant label API – using the below API where we will add backslash from the document library, still, the API will return all files from the given folder.

Example:

https://globalsharepoint2020.sharepoint.com/_api/web/GetFolderByServerRelativeURL('/Shared Documents/Folder1')/files
Get all files from a given folder in SharePoint root site document library using the REST API
Get all files from a given folder in the SharePoint root site document library using the REST API

Key takeaways – Server relative urls must start with (REST API Errors)

  • We need to add backslash (“/”) before the document library if we are using this API from other sites than the tenant root site, for example – https://globalsharepoint2020.sharepoint.com/sites/GSDRnD
  • In the case of the tenant root site, it doesn’t matter whether we are using the backslash (“/”) before the document library or not.

Realtime use of /_api/web/GetFolderByServerRelativeURL API in SharePoint Online (REST API Errors)

For example, if you are a SharePoint developer and want to get all files from the SharePoint document library specified folder which you want to integrate into your codings like Dot Net Application or Power Automate or other programming languages for the further processing of the documents where we need to get all metadata for a given file, document library, site, etc.

In order to get these metadata details, we generally use either the postman tool or Graph API explorer but these are quite complicated to use and time-consuming processes (the Postman tool is more complicated than Graph API explorer). In this scenario, if you have access to the site, you can just simply query the get files API in the browser itself which will return all files along with the metadata.

For your reference, we have added the complete XML file, so that you can refer to those metadata names while you are dealing with the SharePoint file metadata in the coding or programming.

<?xml version="1.0" encoding="utf-8"?><feed xml:base="https://globalsharepoint2020.sharepoint.com/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"><id>771e5d20-22ca-443f-8f4f-9091b4f1ce6a</id><title /><updated>2022-06-11T08:29:48Z</updated><entry><id>https://globalsharepoint2020.sharepoint.com/_api/Web/GetFileByServerRelativePath(decodedurl='/Shared Documents/Folder1/Test file 1.docx')</id><category term="SP.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /><link rel="edit" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Author" type="application/atom+xml;type=entry" title="Author" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/Author" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CheckedOutByUser" type="application/atom+xml;type=entry" title="CheckedOutByUser" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/CheckedOutByUser" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/EffectiveInformationRightsManagementSettings" type="application/atom+xml;type=entry" title="EffectiveInformationRightsManagementSettings" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/EffectiveInformationRightsManagementSettings" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/InformationRightsManagementSettings" type="application/atom+xml;type=entry" title="InformationRightsManagementSettings" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/InformationRightsManagementSettings" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ListItemAllFields" type="application/atom+xml;type=entry" title="ListItemAllFields" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/ListItemAllFields" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/LockedByUser" type="application/atom+xml;type=entry" title="LockedByUser" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/LockedByUser" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/ModifiedBy" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Properties" type="application/atom+xml;type=entry" title="Properties" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/Properties" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/VersionEvents" type="application/atom+xml;type=feed" title="VersionEvents" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/VersionEvents" /><link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Versions" type="application/atom+xml;type=feed" title="Versions" href="Web/GetFileByServerRelativePath(decodedurl='/Shared%20Documents/Folder1/Test%20file%201.docx')/Versions" /><title /><updated>2022-06-11T08:29:48Z</updated><author><name /></author><content type="application/xml"><m:properties><d:CheckInComment></d:CheckInComment><d:CheckOutType m:type="Edm.Int32">2</d:CheckOutType><d:ContentTag>{C9B7D365-810A-4571-AB7D-2AB51EED4F4A},6,6</d:ContentTag><d:CustomizedPageStatus m:type="Edm.Int32">0</d:CustomizedPageStatus><d:ETag>"{C9B7D365-810A-4571-AB7D-2AB51EED4F4A},6"</d:ETag><d:Exists m:type="Edm.Boolean">true</d:Exists><d:IrmEnabled m:type="Edm.Boolean">false</d:IrmEnabled><d:Length m:type="Edm.Int64">17427</d:Length><d:Level m:type="Edm.Byte">1</d:Level><d:LinkingUri>https://globalsharepoint2020.sharepoint.com/Shared%20Documents/Folder1/Test%20file%201.docx?d=wc9b7d365810a4571ab7d2ab51eed4f4a</d:LinkingUri><d:LinkingUrl>https://globalsharepoint2020.sharepoint.com/Shared Documents/Folder1/Test file 1.docx?d=wc9b7d365810a4571ab7d2ab51eed4f4a</d:LinkingUrl><d:MajorVersion m:type="Edm.Int32">3</d:MajorVersion><d:MinorVersion m:type="Edm.Int32">0</d:MinorVersion><d:Name>Test file 1.docx</d:Name><d:ServerRelativeUrl>/Shared Documents/Folder1/Test file 1.docx</d:ServerRelativeUrl><d:TimeCreated m:type="Edm.DateTime">2022-06-11T08:06:46Z</d:TimeCreated><d:TimeLastModified m:type="Edm.DateTime">2022-06-11T08:07:22Z</d:TimeLastModified><d:Title></d:Title><d:UIVersion m:type="Edm.Int32">1536</d:UIVersion><d:UIVersionLabel>3.0</d:UIVersionLabel><d:UniqueId m:type="Edm.Guid">c9b7d365-810a-4571-ab7d-2ab51eed4f4a</d:UniqueId></m:properties></content></entry></feed>

Summary: Server relative urls must start with (GetFolderByServerRelativeURL API)

Thus, in this article, we have learned the below with respect to getting files from the SharePoint document library using the GetFolderByServerRelativeURL API:

  • How to get all files from a given folder in the SharePoint document library using the REST API.
  • How to fix the error “Server relative urls must start with SPWeb.ServerRelativeUrl” in SharePoint Online

See Also: SharePoint Online troubleshooting

You may also like the below SharePoint Online troubleshooting articles:

About Post Author

Do you have a better solution or question on this topic? Please leave a comment