Wednesday, 25 April 2012

Get the list items using CAML from SharePoint 2010 List


Get the list items in SharePoint 2010 List


The caml query to get the list items from the SharePoint 2010 list.


This is the Sample List - List name is "Details"

ID    Title         Address    

1      B               Bangalore
2      C               Chennai


Scenario 1

SPList olist = web.Lists[“ Details ”];
SPQuery spQuery = new SPQuery();
spQuery.Query = “<Where><Eq><FieldRef Name="Title" />
                 <Value Type="Text">B</Value>
                 </Eq></Where>
SPListItemCollection oitems = olist.GetItems(spQuery);


The Result is


ID    Title         Address    

1      B             Bangalore


Scenario 2

SPList olist = web.Lists[" Details ”];
SPQuery spQuery = new SPQuery();
spQuery.Query = “<Where><Eq><FieldRef Name="Title" />
                 <Value Type="Text"> C</Value>
                 </Eq></Where>” 
SPListItemCollection oitems = olist.GetItems(spQuery);


The Result is


ID    Title         Address    

2      C             Chennai




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; 

Monday, 20 February 2012

Get Top 5 list items from Sharepoint List using CAML

Get Top 5 list items from Sharepoint List using CAML


This is a sample code for retrieving top 5 items from the SharePoint List.

SPQuery is the SharePoint class to initialize the CAML query.

This following Query will display the top 5 items OrderBy ID as Ascending is false.

SPQuery spQuery = new SPQuery();
spQuery.Query = "<Query>
   
<OrderBy>
     
<FieldRef Name='ID' Ascending='False' />
   
</OrderBy> </Query> ";
spQuery.RowLimit = 5;