SharePoint CAML Query and Scope attributes

Below article is above understanding scope attribute in CAML query. Suppose you have a document library where documents are stored with some folder structure.

Understanding Scope and its possible values

Default  – No specifying scope attributes, it will only returns all the files under root folder of document library. It won’t go return any files under a folder though applicable based on where condition

Recursive – By specifying scope as Recursive, it will returns files under document library including files under sub folders but not at second  level folder(folder inside folder).

FilesOnly – By specifying scope as FilesOnly, it will returns files only and exclude folders.

RecursiveAll – By specifying scope as RecursiveAll, it will returns all the files under document library including all the folder and sub folder inside folder.

Below are couple of scenarios to understand the same.

Scenario 1 – Retrieve file under specific folder, here folder path is “/folder/subfolder1” is relative path. Please note here example is based on Managed CSOM

List DocumentsList = clientContext.Web.Lists.GetByTitle("SharedDocuments");
CamlQuery camlQuery = new CamlQuery();
camlQuery = new CamlQuery();
camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                    <FieldRef Name='FileDirRef'/>
                                    <Value Type='Text'>
                        <RowLimit Paged='TRUE'> 30 </RowLimit>
ListItemCollection listItems = DocumentsList.GetItems (camlQuery);

Scenario 2 – Find all zip files within a document library, please note here example is based on JSOM.

var files = [];
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('SharedDocuments');    
var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml("<View Scope='RecursiveAll'><Query><Where>" + 
                "" +
                "<Contains><FieldRef Name='FileLeafRef' /><Value Type='Text'>.zip</Value></Contains>" + 
this.collListItem = oList.getItems(camlQuery);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

function onQuerySucceeded(sender, args) {
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();  

function onQueryFailed(sender, args) {
    //Handle Error

Hope this  helps…Happy Coding..!!!

(Visited 1,687 times, 1 visits today)