Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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,

share|improve this question
    
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 at 2:29

2 Answers 2

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

camlQuery.set_folderServerRelativeUrl(folderPath); 
share|improve this answer

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());
    }
);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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