1

I'm working with CAML Designer 2013 to create a query capable of returning all folders and subfolders within a SharePoint 2013 document library. I've gotten a working solution within the designer:

   <ViewFields>
      <FieldRef Name='FileLeafRef' />
      <FieldRef Name='FileRef' />
   </ViewFields>
   <OrderBy>
      <FieldRef Name='FileRef' />
   </OrderBy>
   <QueryOptions>
      <ViewAttributes Scope='RecursiveAll' />
   </QueryOptions>
   <Where>
      <Eq>
         <FieldRef Name='FSObjType' />
         <Value Type='Integer'>1</Value>
      </Eq>
   </Where>

The resulting dataset gives me all folders at all levels, just as I expect. The problem becomes evident when I copy the CAML--as is--into my JavaScript. As soon as it leaves the Designer, my JavaScript only returns the top level folder information. Here's what the actual JS looks like:

  $().SPServices({
    operation: 'GetListItems',
    async: false,
    listName: 'Division Documents',
    CAMLViewFields: '',
    CAMLQuery: '<Query><ViewFields><FieldRef Name="FileLeafRef" /><FieldRef Name="FileRef" /></ViewFields><OrderBy><FieldRef Name="FileRef" /></OrderBy><QueryOptions><ViewAttributes Scope="RecursiveAll" /></QueryOptions><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query>',

    completefunc: function (xData, Status) {
      var fileNames = [];

      $(xData.responseXML).SPFilterNode('z:row').each(function() {
          var fileName = $(this).attr('ows_FileLeafRef');
          fileName = fileName.substring(fileName.indexOf(';#') + 2);
          fileNames.push(fileName);
      });

      var myStuff = fileNames;

      //$('#recentlyModified').append(htmlBuilder);
    }
  });

For example, given this structure in the library:

Folder 1
  Subfolder 1.1
  Subfolder 1.2
Folder 2
  Subfolder 2.1

I get five results (Folder 1, Subfolder 1.1, Subfolder 1.2, Folder 2, Subfolder 2.1) in the designer, and only two (Folder 1 and Folder 2) in JavaScript.

My question for the community: do I need to add anything to the JS to get it to retrieve all levels? I've tried tons of alternatives to syntax, and nothing seems to work yet.

2

When it does that, it generally means you have malformed CAML. You'd want to do it like this:

  $().SPServices({
    operation: 'GetListItems',
    async: false,
    listName: 'Division Documents',
    CAMLQueryOptions: '<QueryOptions><ViewAttributes Scope="RecursiveAll" /></QueryOptions>',
    CAMLViewFields: '<ViewFields><FieldRef Name="FileLeafRef" /><FieldRef Name="FileRef" /></ViewFields>',
    CAMLQuery: '<Query><OrderBy><FieldRef Name="FileRef" /></OrderBy><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query>',

    completefunc: function (xData, Status) {
      var fileNames = [];

      $(xData.responseXML).SPFilterNode('z:row').each(function() {
          var fileName = $(this).attr('ows_FileLeafRef');
          fileName = fileName.substring(fileName.indexOf(';#') + 2);
          fileNames.push(fileName);
      });

      var myStuff = fileNames;

      //$('#recentlyModified').append(htmlBuilder);
    }
  });

Look at some of the comments on the documentation page or in the discussions on how it works.

  • That was it! I'll take a look at the documentation once I get this piece going. Thanks for the assist, @Eric Alexander! – AurumPotabile Oct 28 '16 at 13:05

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.