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);
    }
}

2 comments:

  1. What is query,where u get the list items please mention

    ReplyDelete
  2. Query is SPQuery query = new SPQuery();
    query.ViewAttributes = "Scope=\"Recursive\""
    By specifying scope=recursive it will fetch all the items within the folders too.

    ReplyDelete