I am looking for an efficient way to programmatically find all SharePoint lists with a given template / type in the current site collection from within a sandbox solution.

NB: I'm obviously aware that this can be achieved by iterating through each of the SPWeb.Lists properties, however this doesn't sound very performance-savvy.

I initially though I could use the SPSiteDataQuery class as it allows you to select items from across multiple lists (plus restrict the query by list template ID and define the search scope), however I don't need the items from the retrieved lists -- just information about the lists themselves. I suppose I could grab the List ID from each of the returned results and do a lookup to get the SPList object, but that seems even more taxing.

I am merely trying to populate a dropdown menu in a custom "slideshow" web part's property tool pane with a list of all the picture libraries in the site which the user can select to display images from. Ideas?

  • There are that few lists I don't think it matters iterating them, you can also just cache the results. SPSiteDataQuery method you suggested would work, but it is far more operations and more memory so no benefit at all. You COULD create a search scope to read list names, maybe set the template id there as a scope filter, haven't checked that one though.
    – Hugh Wood
    Nov 6, 2012 at 9:58
  • have you wrote the code for this? I did some speed tests and with 50 lists in place the process was under 1/100th of a second on my test machine of a single quad core blade running hyper v.
    – Hugh Wood
    Nov 8, 2012 at 15:59
  • Hi Hugh. I ended up just using a foreach loop to iterate through the SPWeb.Lists collection and checking each list's BaseTemplate value to get all the picture and asset libraries. The only reason we went with this approach is because I was able to change the web part requirements so that it just displays a selection of picture libraries in the current site (as apposed to the whole site collection). This is actually a major factor for us as our entire intranet is just a single site collection, and so it contains a few hundred child SPWebs -- and iterating all those could've taken ages!
    – user4545
    Nov 9, 2012 at 14:12
  • It's what I would of done, at the end of the day it can't of taken all to long. Wouldn't mind knowing your real world time though and the code used!
    – Hugh Wood
    Nov 12, 2012 at 17:25
  • could you post the answer and the algorithm you used to achieve your results? If you feel it is slow, perhaps someone could see a way of improving it.
    – Hugh Wood
    Nov 19, 2012 at 8:45

1 Answer 1


I ultimately ended up just using a foreach loop to iterate through the SPWeb.Lists collection and then check each list's BaseTemplate value to get all the picture and asset libraries. Code provided below:

    private void PopulateImageLibraryDropDownList()

        _dropDownList.Items.Add(new ListItem("None", string.Empty));

        foreach (SPList list in SPContext.Current.Web.Lists)
            if (!list.Hidden &
                list.BaseType == SPBaseType.DocumentLibrary &
                    (int)list.BaseTemplate == 109 |     // Picture Library
                    (int)list.BaseTemplate == 851       // Asset Library
                _dropDownList.Items.Add(new ListItem(list.Title, list.ID.ToString()));

Note: The BaseTemplate property is of type SPListTemplateType which is an enum containing the template ID's of a number of the OOTB list definitions. The Asset Library however, is not included in the enum.

I was also able to change therequirements of our web part somewhat so that it only displays a list of applicable picture and asset libraries in the current site (i.e. not the entire site collection).

  • It's looking good :) I will bookmark as the way to do it for now, if I ever find a better way I will update this question on my answer.
    – Hugh Wood
    Nov 21, 2012 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.