Monday 29 December 2014

Retrieving SharePoint List Data using CAML


Basic CAML query properties  used in sharepoint 

  • Group by        :-  used to group the data in list view 
  • Order by        :-   used  to sort the list either in ASCENDING or DECENDING
  • Row limit       :-  used to set the limit of item returned from qury 
  • View fields     :-  used to filter fields from the list 
  • View              :-  section which usally surrounds all of the section
  • Query             :-   used to pass the query inside view section
  • OR/AND         :-  Used within the ‘Where’ element to group filters in a query for a   view
Example: OrderBy

<OrderBy> 
     <FieldRef Name="Modified" Ascending="FALSE"></FieldRef>
</OrderBy>

Example: GroupBy

<GroupBy> 
      <FieldRef Name="Modified"/> 
</GroupBy>

Example: AND

    Dim oSb As New System.Text.StringBuilder
    oSb.Append("  <Where>")
    oSb.Append("      <And> ")
    oSb.Append("                   <Eq>")
    oSb.Append("                        <FieldRef Name=""Displayed"" />")
    oSb.Append("                        <Value Type=""Boolean"">1</Value>")
    oSb.Append("                   </Eq>")
    oSb.Append("                   <Neq>")
    oSb.Append("                        <FieldRef Name=""Booking_x0020_Status"" />")
    oSb.Append("                        <Value Type=""Text"">Cancelled</Value>")
    oSb.Append("                   </Neq>")
 oSb.Append("      </And> ")
    oSb.Append("   </Where>")
    Dim sResult As String = oSb.ToString()
    Dim query As New SPQuery()
    query.Query = sResult
    Dim _waitingcoll As SPListItemCollection = _waitinglist.GetItems(query) 

Example: OR

  Dim oSb As New System.Text.StringBuilder
    oSb.Append("  <Where>")
    oSb.Append("      <Or> ")
    oSb.Append("                   <Eq>")
    oSb.Append("                        <FieldRef Name=""Displayed"" />")
    oSb.Append("                        <Value Type=""Boolean"">1</Value>")
    oSb.Append("                   </Eq>")
    oSb.Append("                   <Neq>")
    oSb.Append("                        <FieldRef Name=""Booking_x0020_Status"" />")
    oSb.Append("                        <Value Type=""Text"">Cancelled</Value>")
    oSb.Append("                   </Neq>")
    oSb.Append("      </Or> ")
    oSb.Append("   </Where>")
    Dim sResult As String = oSb.ToString()
    Dim query As New SPQuery()
    query.Query = sResult
    Dim _waitingcoll As SPListItemCollection = _waitinglist.GetItems(query) 

Example Begin'swith / Contains


<Where>
     <And>
       <BeginsWith>
              <FieldRef Name="Conference"/> 
              <Value Type="Note">Morning</Value>
       </BeginsWith>
       <Contains>
              <FieldRef Name="Conference" />
              <Value Type="Note">discussion session</Value>
       </Contains>
   </And>
</Where>

The full Code snippets will look like:

<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>
Share:

No comments:

Post a Comment

© TBGsharepointforum All rights reserved | Designed by Blogger Templates