Skip to main content

Thinking out aloud - Dave Hunter's SharePoint Blog

Go Search
Home
Blog
  

Locations of visitors to this page


 Useful Links

  Microsoft UK events
  SharePoint 2007 on CodePlex
  SharePoint Community Portal
  SharePoint User Group
  SharePoint Community Portal
  SharePoint Pedia
  SharePoint MSDN Forums
  SharePoint University
Home > Thinking out aloud - Dave Hunter's SharePoint Blog > Posts > Tip - Creating CAML Queries
My thoughts and findings on Microsoft Information worker technologies, including MCMS and SharePoint 2007 (MOSS).
Tip - Creating CAML Queries
CAML queries are used with SPQuery and SPSiteDataQuery.  This is a faster and more efficient way of retrieving items based on known criteria rather than using for each on the SPListItemsCollection and checking for the criteria.
 
The problem is CAML query syntax / schema is complicated.  The CAML Query schema is here http://msdn2.microsoft.com/en-us/library/ms467521.aspx.  It basically consists of:
 
<Where>
  <Operator>
    <Operand></Operand>
  </Operator>
</Where>
 
Tip - Constructing a query
 
Lists are also defined with CAML.  So here's the idea, define a view that filters the list accordingly.  Save the list as a template and extract the CAML from the schema file.  Here's the steps ...
  1. View the list in SharePoint.
  2. Create a new view and name it something obvisious ie "fred".
  3. Config the list to include the filter (where clause), fields to view and sorting.
  4. Save the list.
  5. Save the list as a template.
  6. Download the .stp file.
  7. Rename as a cab.
  8. Open the cab file and extract the manifest.
  9. Open the manifest in notepad.
  10. Search for "<Query>".  You will find the CAML required to perform the query you defined in the view (usually this is the second <Query> in the manifest).

Using this method it will allow you to test the query against items in the list that should match or fail ... your own pre-built test harness.  Just add items in and apply the view.

CAML queries can be performed against a list or a site or site collection.
 
Against a list use SPQuery.  Define the CAML Query in the SPQuery.Query property and run using list.GetItems(query);.  This returns a SPListItemCollection of the results.
 
Against a site or site collection use a SPSiteDataQuery.  This is very similar to the SPQuery but has the scope of the current site collection, which can be limited by providing filtering on the SPSiteDataQuery.  This returns a DataTable.
 
Happy programming!
 
  Copyright
This work is licenced under a Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License
  Site Map

Comments

Good Idea

Thats the good idea but better to use U2U CAML Query Builder tool available on codeplex
at 13/02/2009 11:03

Tip - Creating CAML Queries

Think of it as simislar to Revers Polish Logic.
select * from list
where var1 = val1
and var2 = val2
or var3 = val3

var1, val1, <eq>, var2, val2, <eq>, <and>, var3, val3, <or> <where>

then reverse is

<where>
  <or>
     <eq>var3 = val3</eq>
     <and>
        <eq>var2 = val2</eq>
        <eq>var1 = val1</eq>
     </and>
  </or>
</where>

at 19/03/2009 19:10

 Dave Hunter

I'm currently a Senior Consultant at Netstore 2e2. I specialise in Microsoft Information Worker technologies (especially MOSS, MCMS and .NET), with over 8 years of experience within this area of specialism. 

I have gained two certifications for MOSS and WSS and look to complete the full certification track soon.

View Dave Hunter's profile on LinkedIn

  Subscribe in a reader


 Latest Posts

I’m attending the first ever SharePoint Saturday in the UK31/08/2010 19:43
I’ve re-joined CIBER UK31/08/2010 19:26
I’ve been Awarded a MVP for SharePoint01/04/2010 19:49
SharePoint 2010 Training on Microsoft E-Learning15/03/2010 17:56
SharePoint UK User Group - Thursday 27th August London Meeting25/08/2009 17:56
CAML Query that filters on the current user23/07/2009 17:50
Microsoft Ramp Up Free SharePoint Developer Training22/07/2009 20:59
How To: Change a SharePoint Application Pool Programmatically07/07/2009 18:08
SharePoint Forums Topping 100 Answers06/07/2009 12:29
Find out the SharePoint Internal Name for a Column or Site Column28/05/2009 09:56
1 - 10 Next