• Home
  • /Archive
  • /Example CAML queries for SharePoint Designer

Example CAML queries for SharePoint Designer

I have recently been working on a project the deals heavily with XSLT and DVWPs. One thing to keep in mind is that data manipulation done in XSLT is done client side and thus slows load times. This is especially the case if you are just the equivalent of a SELECT * in your CAML query as the entire dataset is returned and then it is up to the client to filter, group, order and display the data.

Spare your audience some load time and filter as much as you can from the dataset via your CAML query. There are number of useful sites out there that explain CAML in detail so I will give a quick and dirty on the queries I am sharing and then provide the queries. Reference

A basic SELECT * CAML query looks like this: <View></View> and for those of you who had to learn LISP in school, these queries will bring back those fond memories. While they are not exactly the same, the recursive nature is apparent.

If you want to filter out what is returned you have to add, similar to T-SQL, a WHERE clause. This looks something like this:

<View>
     <Query>
          <Where>
               Your clause
          </Where>
     </Query>
</View>

And when saying WHERE something AND something else you just add an AND in there:
<View>
     <Query>
          <And>
               <Where>
                    Your clause
               </Where>
               <Where>
                    Your clause
               </Where>
          </And>
     </Query>
</View>

A few key elements to note are:
<Eq></Eq> – Equal To
<Neq></Neq> – Not Equal To
<Gt></Gt> – Greater Than
<Geq></Geq> – Greater Than Or Equal To
<Lt></Lt> – Less Than
<Leq></Leq> – Less Than Or Equal To
<IsNull></IsNull>
<BeginsWith></BeginsWith>
<Contains></Contains>

Each of these conditionals follows this format:
<Eq>
     <Eq>
          <FieldRef Name="Closed"/>
          <Value Type="Integer">
               1
          </Value>
     </Eq>
</Eq>

Finally some useful CAML Queries for SharePoint:

Select all records created by the current user (change Eq to Neq to select all records not created by the current user):
<View>
     <Query>
          <Where>
               <Eq>
                    <FieldRef Name="Author"/>
                    <Value Type="Integer">
                              <UserID/>
                    </Value>
               </Eq>
          </Where>
     </Query>
</View>

All records created by current user and matching some criteria:

<View>
     <Query>
          <Where>
               <And>
                    <Eq>
                         <FieldRef Name="Author"/>
                         <Value Type="Integer">
                                   <UserID/>
                         </Value>
                    </Eq>
                    <Eq>
                         <FieldRef Name="Some Column"/>
                         <Value Type="Text">
                                   Matching Text Here
                         </Value>
                    </Eq>
               </And>
          </Where>
    </Query>
</View>

Finally, be sure to enter the queries into Designer using the HTML form of the >, <, and " signs. I recommend this site here, just copy your CAML query into and it will transform it: http://www.devtrends.com/custom/camlencode/

Also very handy there is an XML Tools plugin to Notepad++ that will do the conversions in Notepad++ for you.

Enjoy.

Leave a Reply

Your email address will not be published. Required fields are marked *

*