🔍

SharePoint CAML Query Builder

Construct XML CAML Queries visually for SharePoint REST APIs, PnP JS, CSOM, or SPFx. Add conditions, select fields, and generate the raw XML instantly.

CAML, and the shape it has to be

CAML(Collaborative Application Markup Language) is the XML query language SharePoint uses for list data. You still meet it in CSOM's CamlQuery.ViewXml, in Get-PnPListItem -Query, and in the REST GetItems endpoint. It is not going away, and it is unusually unforgiving about structure.

Where each element belongs

Only <Where>, <OrderBy> and <GroupBy> go inside <Query>. <ViewFields> and <RowLimit> are children of <View>, as siblings of <Query>. Nest them in the wrong place and SharePoint does not complain — it ignores them, so the query still runs and quietly returns every field and every row. A query that works but is slow and over-fetching is the usual symptom. The generator above emits the correct shape:

<View>
  <Query>
    <Where>...</Where>
  </Query>
  <ViewFields>
    <FieldRef Name="Title" />
  </ViewFields>
  <RowLimit>100</RowLimit>
</View>

FieldRef takes internal names

Name is the column's internal name, fixed when the column was created and never changed by a rename. Spaces are encoded, so a column created as “Due Date” is Due_x0020_Dateforever, even if it now displays as “Deadline”. A query that returns everything, or nothing, while looking correct is almost always a display name that SharePoint could not resolve to a field.

And and Or take exactly two children

This is CAML's sharpest edge. <And> and <Or> are binary, so three conditions require nesting one pair inside another rather than listing three siblings. Four conditions need two levels. The builder above does this nesting for you; written by hand it is the most common reason a query throws on execution rather than on save.

Value types are literal

  • Boolean is 1 or 0, not true or false.
  • DateTime is ISO 8601 (2026-01-31T00:00:00Z), and comparing a time as well as a date needs IncludeTimeValue="TRUE" on the <Value>. Relative dates use <Today />, optionally with OffsetDays.
  • Lookup compares the display text by default. To match the underlying ID, put LookupId="TRUE" on the <FieldRef> and use a numeric value.
  • User fields accept <UserID />as the value to mean the current user, which is how “assigned to me” views are built.

The 5,000 item threshold

Filtering a list of more than 5,000 items on a column that is not indexed throws, regardless of how few rows match — the limit is on rows scanned, not rows returned. Index the filtered column and the same query succeeds. Note also that Contains cannot use an index at all, so it stays slow on large lists where BeginsWith would not.

Frequently Asked Questions

CAML (Collaborative Application Markup Language) is an XML-based query language used to filter, sort, and retrieve data from SharePoint lists and libraries. It's more powerful than OData $filter for complex queries involving nested AND/OR conditions, lookups, and managed metadata.

Use CAML when you need: nested AND/OR logic, lookup column filtering, managed metadata queries, Contains/BeginsWith operators, or recursive folder queries. OData $filter is simpler but cannot handle these advanced scenarios.

Yes! Send a POST request to /_api/web/lists/getbytitle('ListName')/GetItems with a CAML query in the request body. The Content-Type must be application/json;odata=verbose.

Hand-writing CAML XML is extremely error-prone. A single misplaced tag (<Eq>, <FieldRef>, <Value>) will silently return wrong results or throw cryptic SharePoint errors. This builder ensures your nodes are perfectly nested and formatted.

CAML supports: Eq (equals), Neq (not equals), Gt/Geq (greater than), Lt/Leq (less than), Contains, BeginsWith, IsNull/IsNotNull, In (multiple values), and DateRangesOverlap for calendar queries.

Use <FieldRef Name="LookupColumn" LookupId="TRUE" /> with the lookup ID value. This is one area where CAML is superior to OData, which struggles with complex lookup filtering.

Yes. PnP.js provides a .getItemsByCAMLQuery() method that accepts a CAML query string. It handles the REST API call and JSON parsing for you, making it the cleanest approach for SPFx web parts.

We use cookies for analytics and personalised advertising. By accepting, you allow these uses. See our Privacy Policy and Cookie Policy.