Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
DZone MVB

Links

Social

SharePoint for Developers: 3.2 Hands-on - getting list items

>> SharePoint for Developers - Table of contents

To read data from list we have to get reference to list first. SPWeb has lists collection we can use to find the list we are interested in. Following code example shows how to get instance of list.


SPWeb web = SPContext.Current.Web;

SPList list = web.Lists["MyList"];


List, as we can remember, is something like table and something like collection. List has its definition, fields collection and items collection. So, if we want to print out titles of all items in list we can iterate through items collections of list.


private static void PrintItemTitles()

{

    string strUrl = "http://localhost:8099/";

    using (SPSite site = new SPSite(strUrl))

    {

        using (SPWeb web = site.OpenWeb())

        {

            SPList list = web.Lists["MyList"];

            SPListItemCollection items = list.Items;

 

            foreach (SPListItem item in items)

                if (item != null)

                    Console.WriteLine(item.Title);

        }

    }

}


To get item from list we have the following options:

  • asking item directly from collection,
  • asking item by id,
  • querying list to get an item.

Asking item directly from items collection is very resource expensive. If you need one item from large list and you know its numeric ID or GUID then you should never ask item directly from collection. The reason is very simple - to get item from collection by its index then collection must be filled with items first. And if collection is filled you have suddenly all the list items in memory. It is not very nice surprise if list has about 50.000 documents.


SPListItem item = list.Items[itemIndex];


The other option is to get item by its ID. This is done by GetItemById() method.


SPListItem item = list.GetItemById(itemIndex);


If you know item GUID then you can use GetItemByUniqueId() method that works like previous one but uses GUID instead of numeric ID.


SPListItem item = list.GetItemByUniqueId(itemGuid);


These methods are not such resource eaters as asking the whole items collection. Behind the scenes it creates query that returns data only for list item with specified ID and if list item is found then it is constructed and returned.

NB! If you ask Items collection form list then every time all list items will be asked and constructed again. The results are not cached. If you use Items collection then ask it only once - like I did in the previous code example where item titles were printed to console.

I just mentioned querying and you may ask if you can do more with querying than just ask item from list by its ID. Of course, you can. You can query lists by different criterias and we will cover this in next blog entri in this serie.

Comments

funny wallpaper » SharePoint for Developers: 3.2 Hands-on - getting list items said:

Pingback from  funny wallpaper » SharePoint for Developers: 3.2 Hands-on - getting list items

# September 27, 2008 11:26 AM

Vince Zalamea said:

SPList with Foreach performs quite poorly for larger lists.  Have you considered using SPList.GetItems(SPQuery).  

go.microsoft.com/fwlink

# September 27, 2008 6:58 PM

DigiMortal said:

Thank you, Vince, for your reply. I will handle querying in the next entry of this serie. Also I will there that querying is normal way to get data and why it is good plan to avoid using full size item collections.

# September 28, 2008 4:15 AM

Dew Drop – September 28, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop – September 28, 2008 | Alvin Ashcraft's Morning Dew

# September 28, 2008 8:54 AM

YESChandana -Blog said:

My favorite links from the 4th week of September 2008

# September 28, 2008 9:43 AM

My favorite links from the 4th week of September 2008 said:

Pingback from  My favorite links from the 4th week of September 2008

# September 28, 2008 9:43 AM

Links (9/28/2008) « Steve Pietrek - Everything SharePoint said:

Pingback from  Links (9/28/2008) « Steve Pietrek - Everything SharePoint

# September 28, 2008 7:58 PM

Wictor said:

I agree with Vince. Using SPQuery/CAML is the correct way of getting items from a list, your code might look a bit overkill - but the performance increase is overwhelming.

We recently ran into performance trouble, which was not present during the test phase, and the reason was that one of our list which we foreached over had grown to about four times it's size - a quick switch to a simple CAML question solved it, and even made it perform better than ever.

/WW

# September 29, 2008 3:07 AM

Pradeep said:

Crisp and easy to digest posts.

# October 5, 2008 12:30 PM

Querying SharePoint lists « Uncategorized « SharePoint Weekly said:

Pingback from  Querying SharePoint lists « Uncategorized « SharePoint Weekly

# September 3, 2009 7:30 PM

MM said:

Lets say that items 1,2 are returned. On the website, when you click item 1, it opens up another page with the item details. How do we get to the details once we have the list ?

# March 28, 2011 5:07 PM

ps123 said:

Hi ,

i want copy list items to share point library .both list & Lib same site .pls send me code

# January 4, 2012 6:48 AM