I have encountered strange behavior in SharePoint 2013SP when using REST API to retrieve information about documents located Document libraries.
Here is a description of the issue, steps to reproduce are:
- In the top-level site I create a sub-site called 'collab'.
- I import two documents to the Documents library in the top-level site.
- I import a new document to the Documents library in the 'collab' subsite (just one document to somehow differentiate the two libraries).
I did it on a SharePoint 2013SP1 server - freshly installed.
Now, when I query the server to find documents in the 'collab' subsite I use the following REST call:
http://localhost/collab/_api/Web/lists/getByTitle('Documents')/Items
this returns 2 documents which are located in the top-level site Document library and not just the one document from the collab sub-site. Strange.
Exactly same result I get when I run this URL:
http://localhost/_api/Web/lists/getByTitle('Documents')/Items
Why SharePoint does not honor the subsite name in the URL?
At this point I thought that perhaps I am using a wrong endpoint, so I decide to try something else:
http://localhost/collab/_api/Web/GetFolderByServerRelativeUrl('/collab/Shared%20Documents')
This to my surprise returns correct number of documents: 1
Querying the top level site also returns correct number of items: 2.
http://localhost/_api/Web/GetFolderByServerRelativeUrl('/Shared%20Documents')
So now I try to retrieve information about the contents of the Document library of the top level site and the subsite:
First the top-level site:
http://localhost/_api/Web/GetFolderByServerRelativeUrl('/Shared%20Documents')/Files
this request gives correct response, there are two items.
Lets see if I get the same for the sub-site:
http://localhost/collab/_api/Web/GetFolderByServerRelativeUrl('/collab/Shared%20Documents')/Files
Sadly, I get a response which is difficult for me to understand:
<feed xml:base="http://localhost/_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>8f772a2c-2d90-4324-9168-3344aedca14b</id>
<title/>
<updated>2018-01-28T11:37:22Z</updated>
<author>
<name/>
</author>
</feed>
What does this response mean?
When I try a slightly different URL:
http://localhost/collab/_api/Web/GetFolderByServerRelativeUrl('/Shared%20Documents')/Files
I get in the response details about the documents which are in the top level site, even though I have 'collab' site name in the URL. What is going on here?
Questions are:
- How do I retrieve list of documents for the site and subsite Documents library in a consistent manner? What REST API endpoint should I use?
- Why above REST API calls return only those documents which are in the top-level Document library site ignoring the subsite's Document library?
Thanks!
Karol