Wednesday, March 14, 2012

CAML

CAML (Collaborative Application Markup Language) is an XML based markup language used with the family of Microsoft SharePoint technologies. Each markup will start with <View> tag. The classes are:
<View>
 <Query>
  <Where> - Used to specify a filter.
   <And> - Group filters in a query for view. Contain only two filters, if we want to add another we need to wrap both of the element with another <And> element.
   <Or> - Group filters in a query. Contain only two filters, if we want to add another we need to wrap both of the element with another <Or> element.
    <BeginsWith> - Searches for a string at the start of a column that holds Text or Note field type values.
    <Contains> - Searches for a string anywhere within a column that holds Text or Note field type values.
    <DateRangesOverlap> - Used in queries to compare the dates in a recurring event with a specified Date Time value, to determine whether they overlap.
    <Eq> - Arithmetic operator that means "equal to" and is used within a query.
    <Geq> - Arithmetic operator that means "greater than or equal to".
    <Gt> - Arithmetic operator that means "greater than".
    <In> - Specifies whether the value of a list item for the field specified by the FieldRef element is equal to one of the values specified by the Values element.
    <Includes> - If the specified field is a Lookup field that allows multiple values, specifies that the Value element is included in the list item for the field that is specified by the FieldRef element.
    <IsNotNull> - return items that are not empty (Null).
    <IsNull> - return items that are empty (Null).
    <Leq> - Arithmetic operator that means "less than or equal to”.
    <Lt> - Arithmetic operator that means "less than".
    <Neq> -  Arithmetic operator that means "not equal to".
    <NotIncludes> - If the specified field is a Lookup field that allows multiple values, specifies that the Value element is excluded from the list item for the field that is specified by the FieldRef element.

1.       There are two elements for ordering:
<OrderBy> - Determines the sort order for a query. Ordering by defining the elements  “Override” (Optional Boolean) and “UseIndexForOrderBy” (Optional Boolean). For example:
<OrderBy  Override = "TRUE" | "FALSE"  UseIndexForOrderBy = "TRUE" | "FALSE">
  <FieldRef
    Ascending = "TRUE" | "FALSE"
    Name = "Text" />
    ...
</OrderBy>
<GroupBy> - Contains a Group By section for grouping the data returned through a query in a list view .Grouping by defining the element “Collapse”, Optional Boolean. TRUE, for the Group By section in the list view to be collapsed by default. For example:
<GroupBy
  Collapse = "TRUE" | "FALSE">
  <FieldRef Name = "FieldName"/>
</GroupBy>
If we want to group by content type:
<Where>
 <Eq>
  <FieldRef Name='ContentType'/>
  <Value Type='Text'>{ContentTypeName}</Value>
 </Eq>
</Where>
2.       For example, if we want to create a list view that will show us the Title field and the Name field, sort by ID field, and filtered by (Field1 >= 1500) or (field2 <= 500), it will look like this:
<View>
 <Query>
  <OrderBy>
   <FieldRef Name='ID'/>
  </OrderBy>
  <Where>
   <Or>
    <Geq>
     <FieldRef Name='Field1'/>
     <Value Type='Number'>1500</Value>
    </Geq>
    <Leq>
     <FieldRef Name='Field2'/>
     <Value Type='Number'>500</Value>
    </Leq>
   </Or>
  </Where>
 </Query>
 <ViewFields>
  <FieldRef Name='Title'/>
  <FieldRef Name='Name'/>
 </ViewFields>
 <RowLimit>10</RowLimit>
</View>

3.       As I mentioned before about the limit of the <And> and <Or> tags. If we want three filters we will add another <And> / <Or> tag. For example, this is the regular script:
<And>
 <Leq>
 </Leq>
 <Geq>
 <Geq>
</And>
But if we want three, it will look like this:
<And>
 <And>
  <Leq>
  </Leq>
  <Geq>
  <Geq>
 </And>
 <And>
  <Contains>
  </Contains>
 </And>
</And>

No comments:

Post a Comment