• Javascript
  • Python
  • Go

Filtering Folders in CAML Queries: A Step-by-Step Guide

When it comes to retrieving data from SharePoint lists, CAML (Collaborative Application Markup Language) queries are a powerful tool. They a...

When it comes to retrieving data from SharePoint lists, CAML (Collaborative Application Markup Language) queries are a powerful tool. They allow you to specify exactly what data you want to retrieve from a list, making it easier to filter through large amounts of information. One common scenario where CAML queries are useful is when you want to filter folders in a document library. In this step-by-step guide, we will explore how to use CAML queries to filter folders and retrieve specific data from them.

Step 1: Understanding the Folder Structure

Before we dive into the CAML query, it's important to understand the folder structure in SharePoint. In a document library, folders can be created to organize documents. These folders can have subfolders, creating a hierarchical structure. Each folder has a unique URL that can be used in the CAML query to target specific folders.

Step 2: Formatting the CAML Query

To filter folders in a document library, we need to use the <Where> and <Contains> elements in our CAML query. The <Where> element is used to specify the conditions for filtering, while the <Contains> element is used to match a specific value. The basic structure of the CAML query would look like this:

<View>

<Query>

<Where>

<Contains>

<FieldRef Name='FileRef' />

<Value Type='Text'>Folder URL</Value>

</Contains>

</Where>

</Query>

</View>

Step 3: Specifying the Folder URL

In the <Value> element, we need to specify the URL of the folder we want to target. This URL can be obtained by navigating to the folder in the document library and copying the URL from the address bar. Make sure to remove the domain name and only keep the folder URL. For example, if the folder URL is https://sharepoint.com/sites/documents/library/folder1, then the value in the <Value> element should be "library/folder1".

Step 4: Adding Additional Conditions

If you want to filter folders based on specific conditions, you can add additional elements in the <Where> element. For example, if you only want to retrieve folders that contain documents modified in the last 30 days, you can add a <And> element and specify the <Geq> element to match the date. The updated query would look like this:

<View>

<Query>

<Where>

<And>

<Contains>

<FieldRef Name='FileRef' />

<Value Type='Text'>Folder URL</Value>

</Contains>

<Geq>

<FieldRef Name='Modified' />

<Value Type='DateTime'>

<Today OffsetDays='-30' />

</Value>

</Geq>

</And>

</Where>

</Query>

</View>

Step 5: Testing the Query

Once the CAML query is formatted, you can test it by using the SharePoint REST API or any other tool that supports CAML queries. This will allow you to see the results and make any necessary adjustments before using the query in your code.

Step 6: Retrieving Data from Folders

Once the CAML query is tested and working, you can use it in your code to retrieve data from the folders. The query can be used in a CSOM (Client-Side Object Model) or JSOM

Related Articles