locked
SpList.Items not returning items RRS feed

  • Question

  • My custom list "SampleList" has 5 list items. But with the below specified code it does not return any value.

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite siteObj = new SPSite(rootWeb.Site.ID))
       {
          SPWeb webObj = siteObj.RootWeb;
          SPList listObj = webObj.Lists[listName];
          foreach (SPListItem listItem in listObj.Items)
          {
              // Do something
          }
       }
    }

    This list is available for each site collection. For few site collections, the above code works fine. But for few site collections it does not retrieve any item.

    Why are items not retrieved for few site collections?

    Thanks in advance,

    dhijit

    Monday, September 23, 2013 8:10 AM

Answers

  • Your code might be silently throwing an exception on the sites where it fails to get list items. Try accessing your list like this to check it's not null.

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite siteObj = new SPSite(rootWeb.Site.ID))
       {
          SPWeb webObj = siteObj.RootWeb;
          SPList listObj = webObj.Lists.TryGetList(listName);
    	  if(listObj==null)
    	  {
    		//The list is null. Do something
    		return;
    	  }
          foreach (SPListItem listItem in listObj.Items)
          {
              // Do something
          }
       }
    }


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos


    Monday, September 23, 2013 8:38 AM

All replies

  • Hi Dhijit,

    I don't see anything wrong in this code snippet. Try to debug and check if there is any exception.

    Also, the SPWeb.Lists[] expects either the GUID, Title or index. Make sure you are passing the correct List Title.


    Ram Prasad Meenavalli | MCITP | MCTS SharePoint 2010 | MCPD SharePoint 2010 | http://www.spdeveloper.co.in

    Monday, September 23, 2013 8:30 AM
  • Your code might be silently throwing an exception on the sites where it fails to get list items. Try accessing your list like this to check it's not null.

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite siteObj = new SPSite(rootWeb.Site.ID))
       {
          SPWeb webObj = siteObj.RootWeb;
          SPList listObj = webObj.Lists.TryGetList(listName);
    	  if(listObj==null)
    	  {
    		//The list is null. Do something
    		return;
    	  }
          foreach (SPListItem listItem in listObj.Items)
          {
              // Do something
          }
       }
    }


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos


    Monday, September 23, 2013 8:38 AM
  • Please try this

    string siteUrl = SPContext.Current.Web.Url;
    
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
       using (SPSite siteObj = new SPSite(siteUrl))
       {
           using (SPWeb webObj = siteObj.OpenWeb())
           {
              SPList listObj = webObj.Lists[listName];
    
    	  if(listObj != null)
    	  {
                foreach (SPListItem listItem in listObj.Items)
                {
                   // Do something
                 }
    	  }
           }
       }
    }
    

    Regards,


    Sairam Avacorp Technologies

    Monday, September 23, 2013 8:47 AM