SharePoint Lists Webservice – Traverse all Folders and Files

How can we produce a list of all files and folders inside a SharePoint List using the GetListItems method of the Lists.asmx webservice? As the webservice documentation is clearly lacking in some respects here’s an example of how to do it.

This post might seem a bit random but it took me quite some googling before I got the XML correct so I figured this might help someone else facing the same problem.

There’s no need to write a recursive function or anything, we simply specify the ViewAttributes parameter in the CAML query to produce a flat list of all the files (and folders) inside the list. The syntax might be a bit confusing so here’s an example using the List ID:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
      <listName>{ECBA10EA-3E46-4211-A54A-1DD97AAC40BF}</listName>
      <queryOptions>
        <QueryOptions>
           <ViewAttributes Scope="RecursiveAll"/>
         </QueryOptions>
       </queryOptions>
      <rowLimit>100000</rowLimit>
    </GetListItems>
  </soap:Body>
</soap:Envelope>

Without setting the

<ViewAttributes Scope="RecursiveAll"/>

we would only get items at the root level of the List. Note that we also added a rowLimit of 1000 as the standard rowLimit appears to be set to 100 when not specified. As you can see the QueryOptions needs to be embedded inside another queryOptions tag and will fail without it!

Leave a Reply

*

1 comment

  1. vladimir

    100000

    life saving!! thanks so much

Next ArticleBootstrap 2.0 Tabs JQuery Ajax Example