6

I'm trying to query for documents in a specified sub folder of a document library. Using U2U CAML builder, I can generate the query and return successfully within it.

However, when I run the query using set_viewXml it is still returning just the folders at the top level of the document library.

My Query is as follows:

camlQuery.set_viewXml('<View><GetListItems><Query /><ViewFields /><QueryOptions><Folder>' + folderPath + '</Folder><ViewAttributes Scope="Recursive" /></QueryOptions></GetListItems></View>');

Where folderPath is along the lines of 'libraryName/SubFolder/'

Any ideas?

Thanks,

jonvines
  • 161
  • 2
  • 8
  • OK, I've moved the Scope="Recursive" to the View element at the start. And this is now showing all documents within the document library. However, the query is not filtering them on the sub folder name. – jonvines Feb 26 '13 at 10:48
  • 2
    Cracked it, I will propose as answer once time limit has passed:
    camlQuery.set_viewXml('<View Scope="Recursive"><Query></Query></View>');
    camlQuery.set_folderServerRelativeUrl(folderPath);
    
    – jonvines Feb 26 '13 at 11:07
  • 1
    Don't forget to set it as an answer! It's showing up in the top unanswered questions on data.stackexchange.com! http://data.stackexchange.com/sharepoint/query/156248/the-most-viewed-unanswered-questions-for-a-tag – Robert Kaucher Mar 01 '14 at 02:29

2 Answers2

6
camlQuery.set_viewXml('<View Scope="Recursive"><Query></Query></View>');  

camlQuery.set_folderServerRelativeUrl(folderPath); 
Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
jonvines
  • 161
  • 2
  • 8
  • This is a life saver, thanks a lot. The only way I could find to make it work when having > 5000 files/items! – eirikb Dec 07 '17 at 12:21
1

In addition to specifying SP.CamlQuery.folderServerRelativeUrl, there is another way how to filter items by folder in CAML:

//Constructs a query that returns all items under specified folder                    
function createAllItemsInFolderQuery(folderUrl) {
    var qry = new SP.CamlQuery;
    var viewXml = "<View Scope='RecursiveAll'> \
                <Query> \
                    <Where> \
                        <Eq> \
                            <FieldRef Name='FileDirRef' /> \
                            <Value Type='Text'>" + folderUrl + "</Value> \
                        </Eq> \
                    </Where> \
                </Query> \
            </View>";
    qry.set_viewXml(viewXml);        
    return qry;
};

Note: FileDirRef column contains information about the file directory

Example: how to return documents from Documents library under Orders folder

var context = new SP.ClientContext.get_current();
var web = context.get_web();

var list = web.get_lists().getByTitle('Documents');
var qry = createAllItemsInFolderQuery('Shared Documents/Orders'); //Get documents under Orders folder (format: ListUrl/FolderUrl )
var items = list.getItems(qry);
context.load(items);

context.executeQueryAsync(
    function() {
        console.log(items.get_count()); //print number of documents in folder Orders        
    },
    function (sender, args) {
        console.log("Error: " + args.get_message());
    }
);
Vadim Gremyachev
  • 42,498
  • 3
  • 86
  • 167