tags:

views:

220

answers:

2

In sharepoint how can i access all lists the site that have a certain name eg:

SPList list= web.Lists["MyList"]

will get it me at the root, but how can i access all of them that have that name in subsites also.

+3  A: 

You'll have to iterate through all the subsites of the site/site collection and see if that site has a list by the name of MyList in it. The SPWeb.webs property can get you all the sites underneath a site.

Edit

The reason for this is that sites in SharePoint are independent so you can't access resources in another site or site collection without first accessing that site or site collection.

So, to do a something like pulling up all tasks assigned to user, you would need to load every site in every site collection and check the task list to see if it had a task for that user. You can read this to get a better understanding of how Site Collections work. Sites function similarly, but are stored in the same contend database as the root site collection.

Edit 2

Or as pointed out in the comments on here, you can run an SPSiteDataQuery on a site collection so to iterate everything you'd just to have to iterate across site collections.

ICodeForCoffee
can i have more info thanks
raklos
Sure, I've added some more about how sites and site collections work. You can also google around to get a better idea of how the barriers between them work.
ICodeForCoffee
THis is incorrect, using the SPSiteDataQuery class you CAN get all tasks for a user in the entire site collection.
Colin
I didn't know about that. You'd still have to open every site collection and then run an SPSiteDataQuery on it wouldn't you if you were searching for the same list across the farm though. Still that's a big improvement on opening every site in a site collection.
ICodeForCoffee
Yes it is SiteCollection bound, SPSiteDataQuery takes a few properties like BaseType etc. for Lists, a CAML query and a scope, which can be set the SiteCollection. An alternative is FullTextSqlQuery, which uses the SSP´s Search (indexed). By adding mapped properties in the SSP (meaning sitecolumns as a search property) you can talk to the SSP Search directly, the syntax resembles SQL
Colin
+4  A: 

Iterating through SPWebs is not advisable, it would proably have to happen recursively (subsites of subsites of subsites etc.) which makes it very easy to introduce Dispose errors, i.e. forgetting to dispose an SPWeb object.

Do you need the lists so you can access the items in it? If they all have the same type of item in it I suggest you use the SPSiteDataQuery class, which takes a CAML query and does a query over all content in the site collection. You can use the resulting DataTable to display data

Edit: An alternative is FullTextSqlQuery (IF you are using MOSS, not WSS), which uses the SSP´s Search (indexed). By adding mapped properties in the SSP (meaning sitecolumns as a search property) you can talk to the SSP Search directly, the syntax resembles SQL

Colin