5

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,

  • 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! data.stackexchange.com/sharepoint/query/156248/… – Robert Kaucher Mar 1 '14 at 2:29
6
camlQuery.set_viewXml('<View Scope="Recursive"><Query></Query></View>');  

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

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.