Thursday 3 May 2012

Using SPQuery to get data form SharePoint List

SPQuery: This object allows you to construct Queries to return specific items in a List.

Following sample code snippets reads a SharePoint list named "Members" and filters all the approved members using CAML query:

using System.Collections.Generic;

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://sharepointsite").OpenWeb ().Lists ["Members"];

SPQuery query=new SPQuery ();

query.Query = "<Where><Eq><FieldRef Name='Status' /><Value Type='Text'>Approved</Value></Eq></Where>";

SPListItemCollection items=mylist.GetItems (query);

foreach (SPListItem membitem in items )

{

Response.Write(membitem["Name"].ToString ());

Response.Write(membitem["City"]);

}

}

}

}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.