Domanda

I have a set of folders say Folder1 containing Folder2 and Folder3. Folder3 contains Folder4, Folder5 and Folder6.

When i use the OData expand parameter - $expand=Folders/Folders i get only details about Folder 2 and Folder3. I also want to retrieve Folder4,Folder5 and Folder6 in the same call.

The current rest api call is as below

     _api/web/lists(guid'my-library-guid')/RootFolder?$expand=Folders/Folders,Folders/Files

How do i mention that my scope should be recursive?

As per the attached discussion thread, the CAML query provisions setting the scope as recursive to fetch recursively. Please advise if there is the REST equivalent for the same!!

È stato utile?

Soluzione

I think you should use CAML query with REST API.

Here you can find your solution on this answer by Vadim Gremyachev

Sample code provided by Vadim Gremyachev:

function getListItems(webUrl,listTitle, viewXml) 
{
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
    var queryPayload = {  
               'query' : {
                      '__metadata': { 'type': 'SP.CamlQuery' }, 
                      'ViewXml' : viewXml
               }
    };

    return $.ajax({
           url: url,
           method: "POST",
           contentType: "application/json;odata=verbose",
           data: JSON.stringify(queryPayload),
           headers: {
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "Accept": "application/json; odata=verbose"
           }
     });
}

function getAllFolderItems(webUrl,listTitle)
{
    var viewXml = '<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query></View>';
    return getListItems(webUrl,listTitle,viewXml);
}

Your query may like:

<View Scope="RecursiveAll">
     <Query>
         <Where>
            <Eq>
               <FieldRef Name="FSObjType" /><Value Type="Integer">1</Value>
            </Eq>
         </Where>
     </Query>
</View>

Altri suggerimenti

_spPageContextInfo.webAbsoluteUrl + "/_api/Lists/getbyTitle('" + LibraryName + "')/Items?$select=FileLeafRef,FileRef,Id&$top=5000

Yes Folder Data is just giving you a Detail. But you need to Traverse through the URL so you can get Your Root folder. Above url will fetch your records recursively also. no need to expand Folder.

And you can see above Caml Query Example if you change Query var viewXml = ;

so result will be same as per my REST URL.

There is a different approach, in which you can just request for the items within a document library, and add a filter param:

/_api/lists/getbytitle('[your library]')/items?$filter=FSObjType eq 0
  • FSObjType eq 0 -> files
  • FSObjType eq 1 -> folders
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top