SharePoint List items using SPView Programmatically

SharePoint List items using SPView Programmatically

using (SPSite site = new SPSite("http://stec/testweb"))
{
  if (site != null)
  {
    using (SPWeb web = site.OpenWeb())
    {
      SPList list = web.Lists["ListName"];
      if (list != null)
      {
        foreach (SPListItem item in list.Items)
        {
                Console.WriteLine(item.Title.ToString());
        }
      }
      web.Close();
    }
    site.Close();
  }
}


For web part, use following Code for any web in any site collection

using (SPSite site = SPContext.Current.Site)
{
  if (site != null)
  {
     using (SPWeb web = site.OpenWeb())
     {
        SPList list = web.Lists["ListName"];
        if (list != null)
        {
            foreach (SPListItem item in list.Items)
            {
            //Your Code here        
            Response.Write(item.Title.ToString());
            }
        }
        web.Close();
     }
     site.Close();
   }
}


Returns a collection of list items from the list based view.
SPList.GetItems (SPView)

using (SPSite site = new SPSite("http://..SpSiteUrl../myweb"))
{  
 if (site != null)
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPList list = web.Lists["ListName"];
            if (list != null)
            {
                SPListItemCollection coll = web.Lists["ListName"].GetItems(web.Lists["ListName"].Views["ViewName"]);
                foreach (SPListItem item in coll)
                {
                    //Your Code Here
                    Console.WriteLine(item.Title.ToString());
                }
            }
            web.Close();
        }
        site.Close();
    }
}



Ebook Download
View all
Learn
View all