1

I'm using REST to query SharePoint 2013. I need to return a complete list of all files contained in a document library including subfolders. This query itself works correctly using a POST request:

<site collection url>/_api/web/Lists/GetByTitle('MyDocLib')/GetItems?$expand=Folder,File,File/ModifiedBy

with the following CAML query as the payload:

{"query":{        "__metadata": {           "type": "SP.CamlQuery"        },        "ViewXml": "<View Scope=\"RecursiveAll\"><Query><Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Integer\">0<\/Value><\/Eq><\/Where><\/Query><\/View>"     }}

My issue is that SharePoint is ignoring the expand query option for Folder. It properly expands File and File/ModifiedBy.

I'm at a loss here. I need to retrieve the folder information associated with each file so I can present the relative folder to the end user.

I considered doing a second query to retrieve the folder information for each file, but a query to the deferred folder URI returns null data.

So, am I building the query incorrectly? or is the expand query option for Folder broken?

New Information:

I've narrowed the issue to the Items collection. The following query works:

<SP site collection url>/_api/Web/Lists('855b5c5f-2ecc-4747-a78f-b0b8772f64b9')/Items?$expand=folder

It returns a result set of all the items in the collection with the folder attribute properly expanded.

This does not work:

<sp site collection url>_api/Web/Lists('855b5c5f-2ecc-4747-a78f-b0b8772f64b9')/Items(1)?$expand=folder

nor does this:

<sp site collection url>_api/Web/Lists('855b5c5f-2ecc-4747-a78f-b0b8772f64b9')/Items(1)/Folder

The first returns a result set with the indexed item, but the folder attribute is not expanded. The second returns a null set. It seems as though working on indexed items in the item collection is not supported. Is this a bug or intended?

  • what information you need to represent from Folder to the user? – Atish Dipongkor - MVP Dec 3 '15 at 6:38
  • I just need the folder name associated with each file. I can parse the folder name from the ServerRelativeUrl of the File element, but expanding the Folder element should work. – Will Hobday Dec 3 '15 at 18:14
3
_api/Web/Lists('855b5c5f-2ecc-4747-a78f-b0b8772f64b9')/Items(1)?$expand=folder

It does $expand Folder. Just you need to make sure Items(1) is a folder in your library. I have tried it now and I found folder is expanded in the response.

    "Folder": {
        "__metadata": {
            "id": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder",
            "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder",
            "type": "SP.Folder"
        },
        "Files": {
            "__deferred": {
                "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder/Files"
            }
        },
        "ListItemAllFields": {
            "__deferred": {
                "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder/ListItemAllFields"
            }
        },
        "ParentFolder": {
            "__deferred": {
                "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder/ParentFolder"
            }
        },
        "Properties": {
            "__deferred": {
                "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder/Properties"
            }
        },
        "StorageMetrics": {
            "__deferred": {
                "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder/StorageMetrics"
            }
        },
        "Folders": {
            "__deferred": {
                "uri": "https://xxx.sharepoint.com/_api/Web/Lists(guid'63ff3aad-360b-4484-aade-283e603a975e')/Items(13)/Folder/Folders"
            }
        },
        "Exists": true,
        "IsWOPIEnabled": false,
        "ItemCount": 1,
        "Name": "My Doc",
        "ProgID": null,
        "ServerRelativeUrl": "/Shared Documents/My Doc",
        "TimeCreated": "2015-12-03T06:28:05Z",
        "TimeLastModified": "2015-12-03T06:28:20Z",
        "UniqueId": "a5742aab-d01d-4eec-a024-906fee8534f5",
        "WelcomePage": ""
    }
  • Ok, I see the issue. The item collection consists of both files and folders. It's not expanding the folders when doing post Query to getitems. – Will Hobday Dec 3 '15 at 19:25
2

Did you tried the following.

var folderUrl = '/LibraryName/FolderName';
var geturl= _spPageContextInfo.webServerRelativeUrl + "/_api/Web/GetFolderByServerRelativeUrl('" + folderUrl + "')?$expand=Folders,Files";

    $.ajax({
                url: geturl,
                type: "GET",
                contentType: "application/json;odata=verbose",
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                success: function (data) {
                var oresults=data.d.results;

                },
                error: function (jqxr, errorCode, errorThrown) {

                    alert(jqxr.responseText);
                }
            });
  • While that method works to retrieve folder information, it is not suitable for my project. I'm trying to determine why the expand query option doesn't work for folder on an indexed item in the Items collection. – Will Hobday Dec 3 '15 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.