Social Icons

Pages

Tuesday, June 25, 2013

CAML Query

CAML (Collaborative Application Markup Language) is the XML-based language that will help you to perform a data manipulation task in SharePoint. It is easy to relate a list to CAML if you compare it to a database table and query.

CAML Query Comparison Operators:
  • Contains - Contains a given text value 
  • Eq - Equal to
  • Geq - Greater than or equal to
  • Gt - Greater than
  • Leq - Less than or equal to
  • Lt - Less than
  • Neq - Not equal to
  • DateRangesOverlap - Compares dates in recurring events to determine if they overlap
  • IsNotNull - Is not null
  • IsNull  - Is null
CAML Query Basic Example:
2 conditions
        camlQuery.ViewXml = @"<View>
                                   <Query>
                                       <Where>
                                        <And>
                                          <And>
                                                <Eq>
                                                   <FieldRef Name='AssignedTo'/> <Value Type='User'>" + AssignTo + @"</Value>
                                               </Eq>  
                                           </And>
                                                <Leq>
                                                  <FieldRef Name='Priority' /> <Value Type='Text'>" + Priority + @"</Value>
                                                </Leq>
                                      </And>                                 
                                     </Where>
                                   </Query>
                                 </View>";
 3 conditions
        camlQuery.ViewXml = @"<View>
                                   <Query>
                                       <Where>
                                        <And>
                                          <And>
                                                <Eq>
                                                   <FieldRef Name='AssignedTo'/> <Value Type='User'>" + AssignTo + @"</Value>
                                               </Eq>                                    
                                                <Leq>
                                                    <FieldRef Name='Created'/>
                                                    <Value Type='DateTime' IncludeTimeValue='TRUE' >" + Created.ToString("yyyy-MM-ddTHH:mm:ssZ") + @"</Value>    
                                                </Leq>
                                          </And>
                                               <Geq>
                                                <FieldRef Name='Created'/>
                                                <Value Type='DateTime' IncludeTimeValue='TRUE' >" + Created.ToString("yyyy-MM-ddTHH:mm:ssZ") + @"</Value>    
                                              </Geq>
                                      </And>                                 
                                     </Where>
                                   </Query>
                                 </View>";
 
  4 conditions
camlQuery.ViewXml = @"<View>
                                   <Query>
                                       <Where>
                                        <And>
                                          <And>
                                        <Eq>
                                           <FieldRef Name='Reference'/> <Value Type='Text'>" + Reference + @"</Value>    
                                        </Eq>
                                        <Eq>
                                           <FieldRef Name='AssignedTo'/> <Value Type='User'>" + AssignTo + @"</Value>
                                       </Eq>
                                         </And>
                                          <And>
                                        <Eq>
                                        <FieldRef Name='Priority' /> <Value Type='Text'>" + Priority + @"</Value>
                                         </Eq>
                                      <Eq>
                                           <FieldRef Name='Reference'/> <Value Type='Text'>" + Reference + @"</Value>
                                       </Eq>
                                        </And>
                                   </And>
                                     </Where>
                                   </Query>
                              </View>";
 Crete multi condition Query
    private static string CreateCAMLConditions(List<string> conditions, string type)
    {

        // No conditions => empty response
        if (conditions.Count == 0) return "";

        // Initialize temporary variables
        string typeStart = (type == "And" ? "<And>" : "<Or>");
        string typeEnd = (type == "And" ? "</And>" : "</Or>");

        // Build hierarchical structure
        while (conditions.Count >= 2)
        {
            List<string> complexConditions = new List<string>();

            for (int i = 0; i < conditions.Count; i += 2)
            {
                if (conditions.Count == i + 1) // Only one condition left
                    complexConditions.Add(conditions[i]);
                else // Two condotions - merge
                    complexConditions.Add(typeStart + conditions[i] + conditions[i + 1] + typeEnd);
            }

            conditions = complexConditions;
        }
        return conditions[0];
    }
References:
 1. http://sharepoint-works.blogspot.com/2012/05/caml-query-tutorial-for-sharepoint.html#.UclenpyGa3V
2. http://buli.waw.pl/caml-query-multiple-conditions-in-and-or-clause/