Sunday, 18 April 2010

How to Use CAML Query to get Data from SharePoint 2007 List

I was enjoying my summer with some CAML queries – All I wanted was to get some data from a simple Products List in SharePoint 2007. The list was pretty simple with 2 colomns "Product" and "Cost". I used U2U to create a pretty descent CAML query. I created a simple website in VS2008 to test it !!! It all worked fantastically!!!
Here is my Quick Dirty code –
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace WebApplication1
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
SPList mylist=new SPSite ("http://sharepointlogics").OpenWeb ().Lists ["Products"];
SPQuery query=new SPQuery ();
query.Query = "";
SPListItemCollection items=mylist.GetItems (query);

foreach (SPListItem proditem in items )
Response.Write(proditem["Title"].ToString ());



So what it does is, it opens a site called sharepointlogics and then it finds a list called "Products"from the list and then get the item names and Cost out of the list and using a simple Response.Write writes it to a page.

So all you need to do is fire up your Visual Studio, create a simple SharePoint site and a list in the site called "Products" and then copy paste the code on a C# project and run it.

Thats all!!!! it will start fetching values from your sharepoint site and will display you.....
Just a simple starter..........

Hope you would enjoy it !!!!

1 comment:

Anonymous said...


SharePoint Template Sites

  SharePoint look book Get inspired Discover the modern experiences you can build with SharePoint in Microsoft 365 https://lookbook.microsof...