Monday, February 23, 2009

Using Geq and Leq in your CAML queries

If you have worked with SharePoint for very long, more than likely you have come across CAML and wondered why Microsoft created a new language for querying SharePoint list data. You may also have been stuck in the 'CAML loop' where you build a query and run it to find the error 'One or more field types are not installed properly.' This error usually means that you are not using the proper 'internal name' of the field.

CAML is a fairly powerful querying language and can allow you to build fairly complex queries once you understand the schema.

First things first - CAML is case sensitive and is based on XML so it has opening and closing tags.

A simple CAML query would look like this:

<query><where><eq><fieldref
name='Submitted_x0020_By'><value
type='Text'>Me</value></eq></where></query>


Once you have a simple query figured out you can use ANDs and ORs to connect multiple parts of the query together. You simply wrap two Eq queries inside the <And></And> tags (or the Or tags). You can embed multiple Ands and Ors inside each other to create complex queries.


Another set of nodes that I have recently used were <Leq></Leq> and <Geq></Geq> to get back items greater than or equal to or less than or equal to a selected item such as a date.


You can also add items to a list using the Batch and Method commands.


There is an entire list of CAML elements as well as some samples of what you can do with CAML located on MSDN.

No comments:

Post a Comment