Using Generics To House SharePoint Objects

Sometimes you will want to build a query worthy collection of SharePoint objects, something like you want to iterate through all SPList objects that are currently selected in a ListView control (via a checking the ListViewItem.Checked property) in a WinForm application and then perform some operation on them. I don’t event think that last sentence made sense. Let me try this again.

I have been working recently on a SharePoint Permission Manager, where I need a way to stamp an initial collection of SharePoint security constructs, and then arbitrarly let the user select a collection of lists of which to complete the cloning operation against. In order to do this, I had to query a ListView control for the relevant ListViewItems in it, which represented the SPList objects (or their URL’s rather) that were the target of the stamping. This way the user didn’t have to piece meal apply the cloning, but could rather mass clone a master list with the appropriate permissions set to other arbitrary lists within the environment (it works for SPWeb, SPFolder, etc. objects as well).

So, I needed a way to query through my ListView control on my WinForm to harness all the relevant SPList items represented by ListViewItem objects and then perform an operation on them. The best way to do this, IMHO, is to just use a list generic. I should note that the ListViewItems text property I was representing with the URL of the list. To do this is pretty cake:


foreach (ListViewItem item in listView.Items)
List myLists = new List();
string listURL = item.Text;
SPList list = getList(listURL);

foreach (SPList myList in myLists)
// do fun list operations


So in this code we are doing a couple of different things. The first thing that we are doing is iterating through all the ListViewItem objects in the ListView control using a foreach loop. Then, we declare a new System.Collections.Generic.List consuming a SPList object so that we have a strongly typed collection of SPList objects that are indexed, that can be later manipulated if desired however we want. I am declaring a new string which represents the text property of the ListViewItem object, which again is representative of the URL of the SPList object within the control. Then, I am going to harness that by creating a new SPList object, and using the getList method, which is just this:


public SPList getList(string Url)
SPList spList = new SPSite(Url).AllWebs[“/”].GetList(Url);
return spList;


returning the hydrated SPList object, ready to go. Once that object is hydrated, I can add it to the strongly typed collection for later consumption, and for future operations.
Once the collection is defined and full of goodies, I can do whatever I want with it, the example I gave in the above is just using a simple foreach loop to return every SPList object in the collection and then performing some sort of operation on them, probably not incredibly ideal but whatever, its an example. This could be whatever, however in my current circumstances it allowed me to easily apply the permission cloning method that I had previously defined.


No Comments


  1. Links (8/22/2007) « Steve Pietrek’s SharePoint Stuff - [...] **** Using Generics To House SharePoint Objects [...]
  2. 24 Links Today (2007-08-23) - [...] Using Generics To House SharePoint Objects GetList() [...]
  3. Blogger Loser » Blog Archive » Using Generics To House SharePoint Objects - [...] Sometimes you will want to build a query worthy collection of SharePoint objects, something like you want to iterate…

Leave a Reply

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