Skip to main content

Patrick Tisseghem's Blog [MVP SharePoint]

Go Search
U2U Blog Center
U2U website
  

The Happy SharePoint Traveller

U2U Blog Center > Patrick Tisseghem's Blog [MVP SharePoint] > Posts > U2U CAML Query Builder and Execution Tool Released
U2U CAML Query Builder and Execution Tool Released

What is the CAML Builder?

CAML itself is the abbreviation of Collaborative Application Markup Language. It is an XML-based query language that helps you querying, building and customizing Web sites based on Windows SharePoint Services. The XML elements define various aspects of a WSS site. In the first version of our CAML Builder tool, only the query aspect of CAML is worked out. The tool will help you build, test and execute your CAML queries.

We provide you with two things:

  1. A Windows application you can use to connect to your portal or team site and build your CAML query string in a WYSIWYG manner. (U2UCamlCreator.exe)
  2. A small assembly you can reference helping you with the execution of your CAML query string within your code. (U2U.SharePoint.CAML.Server.dll)

CAML builder is downloadable from our SharePoint site. <-- download here!!! You just unzip the file and start working with it. We will work out an official article with more guidance info on the stuff you can do with CAML and the tool. But for the moment, if you want to start, here is a small walkthrough demonstrating the possible use of the tool.

A Small Example

To illustrate the CAML Builder, I will guide you through a small example. Suppose, you have within a WSS team site the following list of products.

You now have to build a small Web part to be displayed on the home page of the team site that displays the results of the following query: 'Get me all products that have a Red color, and a price higher that $500 but less than $1000 and contain the word mountain in the product name and all of this ordered by size'. You can build a nice UI to make the parameters dynamic, but the query itself is not an easy one to construct in CAML without some help. With the CAML Query Builder, it takes only a couple of seconds to build this query string.

Here are the steps

  • Open the CAML Query Builder tool - U2UCamlCreator.exe
  • Enter the URL to your site and either use the credentials of the logged on user or provide ones yourself. Note that the CAML builder internally works completely with the SharePoint Web services and thus can be used on non-SharePoint machines.


     
  • A treeview with the available lists and libraries is displayed.


     
  • Clicking on the list you want to work with displays the fields in another panel.


     
  • The panel above lets you build up your CAML string very easily. For our query, we first select the size field and then check the Order By.


     
  • Next is the part of the color. Clicking the color and the Where checkbox allows us to define the nex part of the query. Just click the small > button to add the new part to the Query string visualized in the treeview at the right.


     
  • Finishing off with the price and the contains in the same manner


     
  • In the lower panel of the window, you will find the CAML query string written out in full. As you notice, it is quite a challenge to construct this string without the help of the builder.


     
  • Of course, we can test our work by clicking the Test button.


     
  • I can copy and paste the query string now to be used within my application. But I do not want to have this string in my code. So what I will do is save the query configuration I just have created in a small XML file that in our application has a .caml extension.


     
  • And then we can close the tool. Note that when you start it again, you can simply open the saved .caml file and continue with the query.
     
  • I am going to build now a small ASP.NET user control to execute this query and show it in a datagrid wrapped up in a Web part to my users.
    I will of course use the SmartPart to host the user control in SharePoint.

    The user control UI is very simple. It just contains a datagrid.


     
  • Executing a CAML query string can be done via the SPQuery class of the WSS object model. Together with the CAML Query Builder tool, we also provide you with a small assembly that encapsulates all of that code. The only thing you need to do is

    - set a reference to U2U.SharePoint.CAML.Server.dll

    - declare a namespace
        
          using U2U.SharePoint.CAML.Server;

    - In the load event-handler, create an object of the CAMLHelper class. You have the option in the constructor to provide it with the site, list and CAML query string in the form of an XML document or just point to the file we have saved using the Query Builder tool itself.

         CAMLHelper hlp = new CAMLHelper(Server.MapPath("~/UserControls/products.caml"));

    - You have two methods on the CAMLHelper. The ExecuteQuery returns you a DataTable, and the ExecuteXMLQuery returns you an XML string.

          DataGrid1.DataSource = hlp.ExecuteQuery();
          DataGrid1.DataBind();

     
  • Deploying our user control and code behind 'the SmartPart way', we end up on our home page with the following result. I also use the property builder of the datagrid to display only a number of the columns returned by the helper.

 

Voilà. In a couple of minutes I have created here a Web part that internally does some fairly complex stuff. Using the CAML Query Builder and the assembly hiding the execution details for me, I can concentrate on the business part of the Web part and not on the plumbing to get it all executed.

I really want to thank Karine Bosch (our CAML girl) who I think has done a great job with the implementation of the CAML Builder. If you have any specific questions regarding the tool, you can contact either her (karine@u2u.be) or myself (patrick@u2u.be). We have a small discussion forum available on the SharePoint site in addition.

What's the future bringing? Well, Karine has started on a small update of the CAML builder to be able to internally switch from the Web Services approach to the Object Model approach. And then she will start concentrating on how we can define which fields we want to have returned instead of returning all of them.

From the U2U side, we are working on a number of other SharePoint and IBF-related tools we will bring to the community. We just need the time to finish them :-)


Comments

Can i do an inner join using this component?

afternoon,

is it possible to query two lists using this component?

i have two lists joined via a lookup and i have been seeking a solution that will allow me to make a query that joins the two lists.

any help would be greatly appreciated
at 2/04/2007 9:05

Can i do an inner join using this component?

afternoon,

is it possible to query two lists using this component?

i have two lists joined via a lookup and i have been seeking a solution that will allow me to make a query that joins the two lists.

any help would be greatly appreciated
at 2/04/2007 9:05

Can i do an inner join using this component?

afternoon,

is it possible to query two lists using this component?

i have two lists joined via a lookup and i have been seeking a solution that will allow me to make a query that joins the two lists.

any help would be greatly appreciated
at 2/04/2007 9:06

Need to format url

If we enter http://localhost/ instead of http://localhost the tool return an error 404 page not found. What's more, there is not help available for such an error on the web. So I think the url should be formatted properly before using and some help should be provided in the blog.
at 16/04/2007 8:12

Bukhara hotel New moon

Our hotel is located in centre of the ancient city Bukhara. Beside us you make magic journey in past . We will meet you with well known Eastern hospitality and high-level service
at 15/06/2007 15:36

Nested conditions

Is is possible to have nested conditions? For example I want to see all the records where the title does NOT begin with 'test'? 
at 30/08/2007 22:24

Inner Join

I am interested in doing an Inner Join using CAML as well if at all possible.  Please post if anyone has information on this.

Thanks
at 2/01/2008 20:23

FileIOException

Hi, tried to run u2u but got a FileIOException when launching the .exe file under Win 2003 server (tried the 2007 version).

Worked fine in Vista 64.
at 31/01/2008 16:36

goodblog

If we enter http://www.green-litehk.com/ instead of http://www.3cmicro.com/ the tool return an error 404 page not found. What's more, there is not help available for such an error on the web. So I think the url should be formatted properly before using and some help should be provided in the blog.
at 7/04/2008 4:36

AddFieldNode with date type

hi,  nice dll! good work!
any more documentation for it?

i am trying to update fields with dateandtime type.

but the AddFieldNode seems to not like it =(
please help.
at 24/04/2008 22:40

How to Download this tool

Hi
How we can download this tool .It is asking user name and password. Pls let me know

My email id : asujikumar@gmail.com
at 11/06/2008 5:46

Error while executing

the server.mappath gives me the error "Data at the root level is invalid. Line 1, position 1." How to troubleshoot the same.........tried every other thing on google 
at 8/07/2008 15:05

CAML Builder Results and Web Service Call Results different

Hi,

I recently used the CAML builder to create the following Query. It returns 1 result from my list (as expected).

<Query>
                <Where>
                    <And>
                        <Lt>
                            <FieldRef Name='Pickup_x0020_Date_x0020_Time' />
                            <Value Type='DateTime' IncludeTimeValue='True'>2008-07-2T08:30:00</Value>
                        </Lt>
                        <Gt>
                            <FieldRef Name='Return_x0020_Date_x0020_Time' />
                            <Value Type='DateTime' IncludeTimeValue='True'>2008-07-2T08:30:00</Value>
                        </Gt>
                    </And>
                </Where>
            </Query>

However, when I try to use this query against the web service call it returns 3 results (the entire list).  I have tried passing in a query option for DateInUTC as true and false, along with different times (regular and UTC) with the same result.  Any help is greatly appreciated.
at 15/07/2008 20:31

CAML works for sort, but in my app it won't

Hi,
I am using your tool to test my sharepoint getlistitems query.  For some reason, I run my query in my app, then paste it in your tool, but the sorting does not work in my app.  It is the exact same query running on the exact same server, but I can't get the sort to work. 

Is there some sort of hidden wrapper or other code I dont' know about?

Here is one of my queries.  (I have tried most fields, modified date, title, and it always sorts by title)

<QueryOptions><Query><OrderBy><FieldRef Name="DocIcon" /></OrderBy></Query></QueryOptions>

(I have also tried it with and without the queryoptions tag.

Please help!!
at 25/07/2008 16:15

good

 enter http://www.tinboxmanufacturer.com instead of http://www.taihingjewelry.com/ the tool return an error 404 page not found. What's more, there is not help available for such an error on the web. So I think the url should be formatted properly before using and some help should be provided in the blog.
at 5/08/2008 10:05

ERROR

Hi all, I am getting that error: Data at the root level is invalid. Line 1, position 1.

Do anyone know what is going on and how to solve that? Thanks
at 8/10/2008 20:32

Same error

I am getting the same error... "Data at the root level is invalid. Line 1, position 1." if anyone knows how to solve that pls email me at renatois@hotmail.com...
at 8/10/2008 21:29

sadfas

nstead of http://www.trisunsoft.com/dtc.htm the tool return an error 404 page not found. What's more, there is not help available for such an error on the web. So I think the url should be formatted properly before using and some help should be provided in the blog.
at 17/11/2008 9:48

Join twl Lists..

Hi, Is it possible to join two lists and generate a CAML Query?
Pls reply ASAP, I need it immediately.

at 22/11/2008 6:37

Re: U2U CAML Query Builder and Execution Tool Released

Not able to download :-(
It's asking UserId and Password.
Please Help
at 3/12/2008 11:47

querying a people or group field

how do you use caml for something like "modified by = [me]"? i tried indicating "[Me]" within the value tag for my caml but this did not work. appreciate your help.

thanks!
at 28/01/2009 10:31

re: querying a people or group field

i was able to resolve my inquiry above by supplying this in the value tag:

<Where><Eq><FieldRef Name="Assigned_x0020_Approver" LookupId="TRUE"/><Value Type="int"><UserID/></Value></Eq></Where>

this translates to Assigned_x0020_Approver = [Me] in the filter section of my view. but within U2U, how do you come up with this query?

looking forward to your response.

-gezelle

email: gezelle@gmail.com
at 28/01/2009 11:39

unable to download your tool

CANNOT download. Asks for username and password.
What is the id and password????!!
at 23/02/2009 22:06

Possible to display the Document URL in the list?

Hello,
it is possible to retrieve and display the document URL with the CAML query?
thanks!
at 5/03/2009 17:55

Username, password required to download?

I cannot download the tool, that list doesn't seem to allow anonymous users!

Any help would be appreciated.
blumenhause@hotmail.com
at 24/03/2009 5:46

Can't download the Tool, User and PW required

It's not possible to download the U2U Caml Tool.

Some help are desireable!
at 2/04/2009 12:00

Can't Download UN and PWD required.

I Can't Download. It asks me a User name and Password?

Can any one help me?

prakash.v@hotmail.com
at 10/04/2009 11:31

It's damn easy

All those who are failed in downloading, I have a trick. Just go to this URL http://
and you will get it.

Thanks for the tutorial , it's helpful.
at 29/04/2009 12:54

Thanks rahul. you are our god

Thanks
at 29/04/2009 12:56

Where is Mexico ?

any one know where is Mexico ?
at 29/04/2009 12:58

I can prove that 1+1=0

Want a solution?
Come to my website.
http://www.santabanta.com
at 29/04/2009 12:59

Still cannot download

"All those who are failed in downloading, I have a trick. Just go to this URL http:// and you will get it."

The URL is stripped - any solution?
at 10/06/2009 12:29
Captcha

Enter the code shown above: *

(Note: If you cannot read the numbers in the above
image, reload the page to generate a new one.)