Getting items from SharePoint lists

In this posting I will show how to get data from SharePoint lists and libraries. There are some ways to do it and it’s only good to know how to select correct method.

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.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    Leave a Reply

    Your email address will not be published. Required fields are marked *