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'>
                        <Query>
                            <Where>
                                <eq>
                                    <FieldRef Name='FileDirRef'/>
                                    <Value Type='Text'>
                                        /folder1/subfolder1
                                    </Value>
                                </eq>
                            </Where>
                        </Query>
                        <RowLimit Paged='TRUE'> 30 </RowLimit>
                    </View>";
ListItemCollection listItems = DocumentsList.GetItems (camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();

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>" + 
                "</Where></Query></View>");
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
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();  
        files.push(oListItem.get_item("FileLeafRef"));
    }    
    console.log(files);
}

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

Hope this  helps…Happy Coding..!!!

(Visited 861 times, 2 visits today)