Saturday, March 7, 2009

SPQuery syntax

SPQuery is the best way to write queries on MOSS.
MOSS is using CAML to get data in the best and quickest way from sites,lists and folder. using SPQuery is in most cases the "best practice" way to find information in MOSS and also used in Sharepoint web services.
We have gather a few impotent information about SPQuery syntax,

  • SPQuery.Query:

  • SPQuery.RowLimit: An int that indicate the row limit.

  • SPQuery.Folder: Get a SPFolder object.  
    This is the best way to get items that are in folder.
  • SPQuery.ViewField: The same is a "SELECT" sentence.


Eq = equal to

Neq = not equal to

BeginsWith = begins with

Contains = contains

Lt = less than

Leq = less than or equal to

Gt = greater than

Geq = greater than or equal to

IsNull = is null

IsNotNull = is not null


Using and & or is very similar to using regular parentheses, between each to operation we need to add AND or OR for example:

  • AND:

We are using AND between 2 operators and add another tab when we add a new operator.

  • OR:

The same as AND.

  • AND & OR:

With AND & OR we are creating brackets, we create each one of the conditions and wrap them with AND/OR.

How to write CAML queries ?

A very useful tool allow you to build yours CAML queries: U2U Caml Query Builder.

Performance consideration:

If your farm is well indexed, retiewing datas using SPquey in the best method to improve performance. See also our post about working with large list in Sharepoint


  1. Hello. I have a question. Can I write some query to find list items that contain some element (key and value) in Properties hashtable?

  2. Hi Alex,

    As far as i know u cannot write a query on the properties hashtable but only on fields.

    Most of the element in the Properties hashtable are fields in the list for example the property "vti_author" is the field Author.