Tag Archive for CAML

Alternate to CAML Or

In is a lesser known CAML operator that I’d like to bring to more peoples attention. When writing CAML and you want to restrict the results by a multi value field equaling two or more values, you’d write something like:

<where>
<or>
  </or><or>
    <eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">Scotland</value></values>
    </eq><eq>
    </eq><eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">England</value></values>
    </eq><eq>
  </eq></or>
  <or>
    <eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">Wales</value></values>
    </eq><eq>
    </eq><eq>
      <fieldref Name="Country"></fieldref><values><value Type="LookupMulti">Ireland</value></values>
    </eq><eq>
  </eq></or>
 
</where>

This will return any item that has the value Scotland, England, Wales or Ireland in the Country field. As the number of values you want to match against grows, imagine the state of the CAML query!
Read more

DateRangesOverlap

When working with calendar items created with recurrence patterns, there’s no immediately obvious way through the object model to expose the individual recurrence items. If you create a calendar event with a daily recurrence pattern which generates 6 separate events, you’re only ever given one item when looking for it through code.

Using CAML, it’s easy to get all 6 items back as individual items through the use of the DateRangesOverlap element.

The structure of the DateRangesOverlap element looks like:

<daterangesoverlap>
  <fieldref Name='EventDate'></fieldref>
  <fieldref Name='EndDate'></fieldref>
  <fieldref Name='RecurrenceID'></fieldref>
  <value IncludeTimeValue=\"TRUE\" Type='DateTime'>
    <now></now>
  </value>
</daterangesoverlap>

Read more

SharePoint CAML Helper Update

A couple of years ago I wrote a CAML helper project and uploaded the source code to CodePlex.

Recently I updated the code with a few small bug fixes, after some feedback from a colleague who used it successfully on a large scale Portal project.

As a result, I thought I’d write a short post to let those who weren’t aware of the code that it was there.

To view the C# source for the tool, or download the latest assembly, please follow this link.

To create CAML queries with the assembly, work with the CAMLManager class to build your query.

CAMLManager mgr = new CAMLManager();
 
mgr.QueryGroups.Add(new QueryGroup("Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "A Title"));
mgr.QueryGroups.Add(new QueryGroup(Types.JoinTypes.Or, "Title", Types.FieldTypes.Text, Types.QueryTypes.Eq, "Another Title"));
 
string caml = mgr.GetCAML();

The GetCAML method will return the following string:

<where>
  <or>
    <eq>
      <fieldref Name="Title"></fieldref>
      <value Type="Text">A Title</value>
    </eq>
    <eq>
      <fieldref Name="Title"></fieldref>
      <value Type="Text">Another Title</value>
    </eq>
  </or>
</where>

Read more

Follow

Get every new post delivered to your Inbox

Join other followers: