CAML Query Builder in SharePoint 2013 Online

caml query builder sharepoint 2013

Here we will discuss CAML as well as the free CAML query builder tool in SharePoint 2013. We will see how we can use the U2U CAML query builder tool step by step. CAML know as Collaborative Markup Language is an XML based language for querying and updating SharePoint objects like List and Libraries. CAML helps us retrieving items from a SharePoint list based on various conditions, we can also put order by, can also limit items returns from the query etc.

Also you may read: Task assigned to user or current user groups in SharePoint 2010 using CAML query

The below is a simple syntax of the CAML query:
<Query>
<Where>
<Eq>
<FieldRef Name=”FieldName” />
<Value Type=”DataType”>Value</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name=”FieldName” />
<FieldRef Name=”FieldName” />
</OrderBy>
</Query>

Here we can use various other operators like below:

Comparison Operators General Meaning
Eq =
Gt >
Lt <
Geq >=
Leq <=
Neq <>
Contains Like
IsNull Null
IsNotNull NotNull
BeginsWith Beginning with word
DateRangesOverlap compare the dates in a recurring event with a specified DateTime value, to determine whether they overlap

Rather than writing the query manually, we can use a free tool known as CAML query builder to generate the query.

Steps to use CAML Query Builder in SharePoint:

This is a great tool to create and test CAML queries. This will work with both on-premise as well as SharePoint online. You should have Microsoft .NET Framework 4.5 installed in your machine else it will not work.

First Download the Tool from this URL and then install it. You can download “U2U Caml Query Builder for SharePoint 2013 (Standalone Edition)”.

Also read: Caml Query with Order by Condition for drop-down columns in SharePoint

Once installation over, when you will open the tool, it will open the Connect dialog box, through which you will be able to connect to your on-premise as well as your online environment. see fig below:

online caml query builder
online caml query builder

Here first I gave my on-premise URL and when I connect it, it displays me all the list and libraries presented in the site like below:

caml query in sharepoint 2013 examples
caml query in sharepoint 2013 examples

Similarly, you can also connect to a SharePoint Online site. When connecting to online site, you have to provide the Username and password. And then the URL.

Enter the URL like https://onlysharepoint2013.sharepoint.com/

If you will add /SitePages/Home.aspx, it will through an error. See fig below:

caml query tutorial for beginners
caml query tutorial for beginners

Here in my SharePoint Online, I have a list name as TestList and I want to build the query for that list.

Now select the List and then New query -> Query with View Fields as shown in the fig below

u2u caml query builder sharepoint online
u2u caml query builder sharepoint online

This will create two tabs one is Query and another one is View Fields. In the View Fields tab you can select the Columns which are you want to query like below: Here I have selected the ID, Title, and First Name columns and it showed me the query in the Editor window.

caml query builder sharepoint
caml query builder sharepoint

Any time you can run the query and can see the results. It will also generate the CSOM Code and Server side code for this which you see from the corresponding tabs.

Read: Retrieve List items from SharePoint 2013 based on ID column using CAML Query

Now let us go to the Query tab and add some filter criteria into the query.

Here I added a Filter that If Title Contains Items and then added Title equals Sharepoint item-1. You can add the Or or And condition by click on the Filter drop-down.

Similarly If you want to add one Order by then you can click on the Add order by element button and then select the Column and the condition whether Ascending or Descending. Then it will generate the query for you in the Editor box.

caml query builder sharepoint 2013
caml query builder sharepoint 2013

caml query builder sharepoint 2013′></div><div><br></div><div>If you want to use the same query in a CSOM code, then you can write like below:</div><div><br></div><div>using (ClientContext ctx = new ClientContext(
{
Web web = ctx.Web;
List list = web.Lists.GetById(new Guid(“5c42def2-bd2c-4df6-bcca-359351944cd2”));
var q = new CamlQuery() { ViewXml = “<View><Query><Where><Or><Contains><FieldRef Name=’Title’ /><Value Type=’Text’>Items</Value></Contains><Eq><FieldRef Name=’Title’ /><Value Type=’Text’>Sharepoint item-1</Value></Eq></Or></Where><OrderBy><FieldRef Name=’ID’ Ascending=’False’ /></OrderBy></Query><ViewFields><FieldRef Name=’ID’ /><FieldRef Name=’First_x0020_Name’ /><FieldRef Name=’LinkTitle’ /></ViewFields><QueryOptions /></View>”};
var r = list.GetItems(q);
ctx.Load(r);
ctx.ExecuteQuery();
}

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →