Typed Dictionary With SharePoint Web Titles And Typed SPListItem Collection

I use this technique a lot when I am building some aggregation components in custom applications. The goal of the method is to provide a static way to constrcut typed Dictionary collections where the SPWeb.Title acts as the Key, and a typed SPListItem collection is the Value. While there will obviously only exist one unique instance of the SPWeb.Title key, additional SPListItem‘s should be appended to their respective key.

In the below, the ItemConsumeAndStructure method has a return type of Dictionary<string, List<SPListItem>>, in accordance with the design goals set forward in the above. There are two parameters being consumed, an IEnumerable<SPList> that represents the set of SPList objects that should be subject to the loop, and a SPView object that’s the view we want to restrict the SPListItemCollection to. Firstly, a temporary dictionary used for the return is built, subsequently a loop will execute against the typed collection hydrating a SPListItemCollection refined using the aforementioned SPView parameter. Recurring through that collection, the dictionary is checked with the SPListItem.SPWeb.Title property is already a key present in the dictionary, and if it’s not instantiate a new List<SPListItem> collection. Adding the SPListItem otherwise to the pre-existing key SPListItem collection completes the requirement.


public static Dictionary> ItemConsumeAndStructure(IEnumerable listofLists, SPView view)Q
var dictionary = new Dictionary>();
foreach (SPList list in listofLists)
SPListItemCollection items = list.GetItems(view);
foreach (SPListItem item in items)
if (!dictionary.ContainsKey(item.Web.Title))
dictionary[item.Web.Title] = new List();
catch (Exception exception)

return dictionary;




  1. Jonas says:

    don’t forget to remove the try catch….

  2. adam says:

    Well, it SHOULD be in there, it’s just generally a developer choice what happens in the latter.

  3. Joe says:

    Thanks alot, I spent a day to find a code that allow me to read data from sharepoint view, and I found here finally,
    thank you so much

  4. adam says:



  1. Links (9/7/2009) « Steve Pietrek – Everything SharePoint - [...] Typed Dictionary With SharePoint Web Titles And Typed SPListItem Collection [...]

Leave a Reply

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