Tuesday 21 February 2012

View Fields In CAML

The sample CAML Query using viewfields

SPQuery query = new SPQuery();
               query.Query = string.Concat(
                              "<Where><Eq>",                                 "<FieldRef Name='Status'/>",
                                 "<Value Type='CHOICE'>Not Started</Value>",
                              "</Eq></Where>",
                              "<OrderBy>",
                                 "<FieldRef Name='DueDate' Ascending='TRUE' />",
                                 "<FieldRef Name=’Priority’ Ascending='TRUE' />",
                              "</OrderBy>");                   

               query.ViewFields = string.Concat(
                                   "<FieldRef Name='AssignedTo' />",
                                   "<FieldRef Name='LinkTitle' />",
                                   "<FieldRef Name='DueDate' />",
                                   "<FieldRef Name='Priority' />");

               query.ViewFieldsOnly = true; // Fetch only the data that we need.


Determine Total Count Of Items Returned By SPQuery


 SPQuery query = new SPQuery();
   query
.Query = string.Concat(
                 
"<Where><Eq>",
                     
"<FieldRef Name='Status'/>",
                     
"<Value Type='CHOICE'>Not Started</Value>",
                 
"</Eq></Where>",
                 
"<OrderBy>",
                     
"<FieldRef Name='DueDate' Ascending='TRUE' />",
                     
"<FieldRef Name=’Priority’ Ascending='TRUE' />",
                 
"</OrderBy>");                  


   
SPListItemCollection items = list.GetItems(query);
   
double totalCount = items.Count; 

4 comments:

  1. wonderful !!! is there any other way to select the particular columns ?

    ReplyDelete
  2. this is the best way to select the columns from sharepoint list

    ReplyDelete
  3. Please fix your template. Dark grey on grey is just not readable. THanks.

    ReplyDelete
    Replies
    1. Thank you for the suggestion, I Will do it

      Delete