Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I feel like I'm very close on this one.

I know how to get the root site collection from this:

How can I get the root site collection from SPContext.Current?

I've been messing around with SPSiteDataQuery, but this seems to be designed to get list items, not information about the parent list.

What I need is to get every SPList for an entire Site. I need to get name, id and parentweb so that a user can select a list, and I can access it directly in the future by SPList:Guid and ParentWeb:Guid

It seems easy to do this recursively by looping through SPContext.Current.Web.Site.WebApplication.Sites and getting AllWebs as shown here:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistcollection.aspx

Is this the best way to do this? It seems quite inefficient.

share|improve this question
    
Why not create all the lists in a single site, if your plan is to bypass the site hierarchy anyway? –  Christophe Oct 4 '12 at 19:08
    
Because this is going to be a Site Definition that can be deployed anywhere, and I'm creating a UI to allow people to pull in content from all over the farm. –  Wesley Oct 4 '12 at 19:13
    
Approximately how many sites and lists are we talking about here? Is it going to be a fixed number or will it grow over time? –  Vardhaman Deshpande Oct 4 '12 at 19:35
    
For the Site Collection, the SPContext you'd want is SPContext.Current.Site.RootWeb - what you have would do all Site Collections in the Web Application. –  lgaud Oct 4 '12 at 19:39

2 Answers 2

up vote 2 down vote accepted

There is no buildin way to get all the lists in a site collection.

Your method of looping through AllWebs and getting the Lists collection for each is the only way, but it's not efficient, so you probably want to build some cache or maintain your own list and updated it using a EventReceiver each time a list is create/delete or when a site is deleted.

But in order for your UI presenting the users with a list of all lists to be useful then your site collection should be very small in which case you might just do looping.

share|improve this answer

Have a look here: SPSiteDataQuery vs. Recursive SPWeb.Lists Performance

I believe your question is very similar to that one.

share|improve this answer
    
Yeah, I've seen that, but that answer is just another question complaining about lack of support. I'm looking either for confirmation that my method is sufficient, or for an alternative that is better. –  Wesley Oct 5 '12 at 4:06
    
Did you try getting the ListItems using SPSiteDataQuery and then doing the SPListItem.ParentList to get the List? I think this approach would be better performance wise. But of course the recommended approach will always be to check the actual performance of the code in your environment. –  Vardhaman Deshpande Oct 5 '12 at 4:50
    
I thought about that, but on a large enterprise farm this could be hundreds of thousands if not millions of list items. Every item would grab this information and send it to a collection where I would have to check for duplicates. Additionally, SPSiteDataQuery returns a datatable, not an SPListItemCollection, so that information would have to be mined to begin with. –  Wesley Oct 5 '12 at 16:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.