Tuesday, April 20, 2010

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

Simple Products List in SharePoint 2007. The list was pretty simple with 2 columns "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!!!

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://sharepointblog").OpenWeb ().Lists ["Products"];
SPQuery query=new SPQuery ();
query.Query = "";
SPListItemCollection items=mylist.GetItems (query);

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

}


}
}
}

No comments:

Post a Comment