Showing posts with label query Sharepoint list using CAML. Show all posts
Showing posts with label query Sharepoint list using CAML. Show all posts

Thursday, October 29, 2009

Querying a SharePoint list using CAML (Collaborative Application Markup Language) builder and updating a List using SharePoint Object model.

To Do: First we must have a U2U CAML Query Builder or one must be familiar with CAML query language syntax. Download CAML builder http://www.u2u.info/SharePoint/U2U%20Community%20Tools/U2U%20Caml%20Query%20Builder%202007%20v3.1.0.0%20(windows%20version).zip
Extract the zip file from above download and
click on U2UCamlCreator.exe which opens the below screen


Now enter the URL of your SharePoint site Ex: http:/servername//sites/mysite/ and use the default settings and click on connect. This will open up all the lists associated with the site as shown below

Now select the desired list on which you want to perform a query. In this case I am selecting a list existing on my site leavenotification and then CAML builder shows all the columns of the list as shown below.



Now select the columns which you require for your application and you can set the where condition to build the query with an option of order by. In my case I am selecting Date From column with a where condition as date lower than or equal as shown below

To DO: 1. Select the column to be filtered

2. Select the Where check box

3. Select a condition for search criteria and click on > button Above 3 steps will generate a XML based CAML query.


To test this query click on Test button and the result will be displayed on a grid as shown below At any point of the time you can change the query by clicking Clear button on top right corner


Using SharePoint object model to query a list with the query generated from CAML builder.
Import Microsoft.sharepoint.dll
Code Part:
Try
{

SPWeb objSPWeb = SPContext.Current.Web.Site.RootWeb;
objSPWeb.AllowUnsafeUpdates = true;
SPList objSPList = objSPWeb.Lists["LeaveNotification"];
SPQuery queryErrData = new SPQuery();
string QueryOrderBy = " 1 "
SPListItemCollection objlistItemsErrLog = objSPList.GetItems(queryErrData);



}
catch
{
throw ;
}
From the above ListItemCollection we can access the result set by looping thru the collection using foreach