How To: Easily construct your CAML queries with CAML Query Builder 

This post is a mere followup to my previous minor posts on the subject of SPQuery and SharePoint queries located here.

Basically this post will provide step-by-step instructions to construct your CAML (Collaborative Application Markup Language) queries using the U2U CALM Query Builder which you can find on the U2U Community Tools page.

Prerequisites

In order to be able to follow along with this step-by-step instructions you'll need the following:

  • U2U CAML Query Builder 2007 or later
  • A site containing a Task List with some items
    • Some items with Status set to Completed
    • Some items with Status set to something else than Completed
Let's get going!
  1. First of all, make sure you've got some items in your Task list:
    image

  2. Launch U2U CAML Query Builder

  3. Fill in the URL to your SharePoint site and choose weather you'd like to go in the Object Model-way or using SharePoint Web Services. I chose Connect via Object Model:
     image

  4. Select "Tasks" in the list that you'll be presented with, and the right-hand side of the application will automatically be filled with the fields availible for that list, which will be the basis for our query:
    image

    As you can see here, there's the option to:
    • Choose the field - the CAML Query Builder will automatically choose the InternalName
    • "Order By" - order the results being returned in any choose order
    • "Where" - Specify which results should be part of the returning resultset, this is the most vital tag if you ask me!
    • "Test Your CAML!"
      • "Editor": Enter any CAML here and hit the "Test" button in order to test the entered query directly against your List
      • "Parameters": Retrieves any parameters
      • "Result": The returning result of the query you test
        (tip: Always test your query before applying it to your code, no matter how simple it may be)

 

  1. Select the "Status" field
  2. Check "Where" and choose "Equal"  and choose the value "Completed" like this:
    image

  3. Hit the little arrow pointing to the right, and the query will be automatically created for you and you'll get the CAML in the bottom-window like this:
    image
    IMPORTANT: When you construct your query in SharePoint later on, REMOVE the <Query> tags. See this post on the subject.

  4. Now that you've created the CAML you want, hit the "Test" button to the right and make sure you get the desired result!
    image
    Note: You can check the column called Status and make sure that all rows returned states "Completed"

  5. Now we've got our CAML Query ready to be implemented in our SharePoint code. Since we didn't make anything complex or advanced it'll be quite easy to implement this query, as I will show you now.
Prepare a Console Application for SharePoint Development

I will for simplicity create a Console Application to retrieve the desired SPListItem objects based on our SharePoint CAML query

  1. Create a new Console Application

  2. Add a reference to Microsoft.SharePoint.dll (Windows SharePoint Services) in order to be able to access the SharePoint object model
    image

  3. Add the following standard using-directive (Microsoft.SharePoint) in order to be able to use the classes in the SharePoint namespace:
    image

  4. Now we're ready to roll - hang on to your camel
Implementing a normal list loop, without queries (not a good approach)

The following example is what it basically looks like at many implementations. A straight through loop that'll basically loop EVERY item in the list and check EVERY object if it's matching what we want - this is not a valid approach if you've got a couple of thousand (or even just a couple of hundred) items in the list. It'll take way too long.

 

image 

The output is correct, but the time to walk through each item may even cause a timeout if you've got many thousand items in the list.
image

Implementing SharePoint (CAML) Queries using the SPQuery object (very good approach)

This will demonstrate how you achieve exactly the same result as previously demonstraded, but with less resource-hogging and much quicker.

Direct advantages:

  • Ordering, you can specify exactly in what order, sorted on any column that you want your resultset to be.
  • Filter out unwanted items (using the Where-tag you can simply choose ONLY the items you want returned)
  • SharePoint will dig right into the list and pick out the correct objects much faster than the previous example

Take a look at the following code, which will implement our previously created query:
image 
IMPORTANT: As you can see in the code above, I didn't use the <Query> and </Query> tags provided by the U2U CAML Query Builder. If you do, your query will not work ;)

Conclusion

You may not acknowledge any performanceboost if you've got up to 100 items in your list, but when the list grows you'll notice a huge difference immediately.

I did a test on my Forum Web Part where I had about 2500 posts (items in a list) and retrieved them using the first approach mentioned here (simply looping all items and using if-statements on each of them) - it resulted in timeouts all the time. When the page loaded, it took more than a minute per request, which isn't appreciated by anyone ;)

When I made the comparsion with my SPQuery implementation to directly pick out the items I wanted, it resulted in a 1 second loadtime instead of more than a minute. This speaks a lot about how good queries are.

Yeah okay.. So what should I do now?

You should most definately refactor all your code to use Queries if you havn't already!

Most people already do use this approach, but since I keep seeing implementations that doesn't, I just thought I'd write this little piece up.

Hope someone can find it useful.

 
Posted on 5-May-08 by Tobias Zimmergren
730 Comments  |  Trackback Url  |  Link to this post | Bookmark this post with:        
Tags: CAML, Queries
 

Links to this post (Trackbacks/Pingbacks)

Allegra motor homes.

Trackback from Allegra d. on 22 Nov 2008 05:04

Comments

Monday, 5 May 2008 01:38 by David Rogers
Hi Zimmergren, good conclusion about using queries. we only just began with it since you tipped us off a while back, and as you say its an incredible performance boost to do so instead of the looping of all items :) thanks for a good blog!

Monday, 5 May 2008 02:55 by Damian Wells
Hey again. good one. i like the caml builder tool to use to create queries. awesome tool they have done. good walkthrough and tips on how to use it. will try to do more queries from now on

Monday, 5 May 2008 08:16 by Dracula
finally your back! missed the pictureposts ;) nailed that one too.

Tuesday, 6 May 2008 09:07 by CAML girl
Hi Tobias, Thanks for mentioning the U2U CAML Query Builder! Nice to read people actually use it. Kind regards, Karine

Wednesday, 7 May 2008 09:31 by Tobias Zimmergren
Hi, thanks for the comments guys. Katrine, no worries. It's an awesome tool when it comes to quickly generate single-list queries. How about extending the functionality with SPSiteDataQuery to get cross list query functionality? ;)

Thursday, 8 May 2008 08:17 by Danielle Watson
how come I never heard of this tool before.. been coding queries with CAML by hand for a year.. This was too easy ;) Thanks a lot for the tip!!

Thursday, 8 May 2008 03:13 by CAML girl
Good idea! I've put it on my agenda but I can't put a date on it: I first want to release a new version of the feature and then for CAML queries needed for GetListItemChanges methods (windows version). Kind regards, Karine

Wednesday, 14 May 2008 04:29 by Damian
hi again. works nice. how about SPSiteDataQuery method. possible in caml query bulder?

Saturday, 11 Oct 2008 09:59 by dick penny
Tobias, can you send me some CAML to use in a WebPart for SP which will return the count of the # of items the query returns? Thanks.

Sunday, 2 Nov 2008 07:08 by 360by2
vLinq is really Great Tool. By using this tool easily create linq query. I came to know about this tool from the blog http://www.elevatesoftsolutions.in/post/2008/11/01/LINQ-Query-Builder-Tools.aspx In this blog one more LINQ Query Builder Tool is mentioned that is LinqPad. Some demerits are in LinqPad that I came to know after using it. Thus I prefer to use vLinq. Good thing about this that It add-in the VS and query generate can be reused. Really Great Tool --- KUnal MEhta The Blog have Fun n FUN http://360by2.blogspot.com

Name:
URL:
Email:
Comments:


MCTS WSS 3.0 (Developer and Administration/Configuration) MCTS MOSS 2007 (Developer and Administration/Configuration) MCP