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<string, List<SPListItem>> ItemConsumeAndStructure(IEnumerable<SPList> listofLists, SPView view)Q
-
{
-
try
-
{
-
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].Add(item);
-
}
-
}
-
}
-
catch (Exception exception)
-
{
-
-
}
-
return dictionary;
-
}
Related posts:
- Enumerating All SPWebs In SPFarm.Local Into Strongly Typed Collection
- Removing Duplicate SPList Objects From Typed Collections
- Best Practice: Don’t Iterate SPListItems for IQueryable Support
- Building Master SPFile WebForm Collections, Remember SPListItems!
- SPListItem.URL Funky Return Fiesta
5 Comments »
RSS feed for comments on this post. TrackBack URL























Articles & Research
SharePoint Architecture
Research Methodology
Personal/Off-Topic
Article Or Research Filed Under 

don’t forget to remove the try catch….
Comment by Jonas — September 4, 2009 @ 10:48 am
Well, it SHOULD be in there, it’s just generally a developer choice what happens in the latter.
Comment by adam — September 4, 2009 @ 11:16 am
[...] Typed Dictionary With SharePoint Web Titles And Typed SPListItem Collection [...]
Pingback by Links (9/7/2009 « Steve Pietrek – Everything SharePoint) — September 7, 2009 @ 4:10 pm
Hi.
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
Comment by Joe — March 27, 2010 @ 12:17 pm
np!!
Comment by adam — April 13, 2010 @ 10:04 am