Thursday, March 3, 2011

How to get list items through Sharepoint Object Model

Programmatically we can get sharepoint list items through Object Model.
It is so easy.

Just we need SPSite, SPWeb instances and get the list out of it. Then we need to formulate SPQuery and get the items.

There you go.

using(SPSite site=new SPSite("Site_URL")
{
   using(SPWeb web=site.OpenWeb())
   {
    SPList list = web.Lists["List_Name"];
    SPQuery query = new SPQuery();
    query.ViewAttributes = "Scope=\"Recursive\"";// this will search your folders too
    SPListCollection newsListColl = list.GetItems(query);
    }
}