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,

3
  • 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, 2013 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, 2013 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! data.stackexchange.com/sharepoint/query/156248/… Mar 1, 2014 at 2:29

2 Answers 2

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

camlQuery.set_folderServerRelativeUrl(folderPath); 
1
  • 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 7, 2017 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());
    }
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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