I have a SharePoint site. I am trying to open a subsite and get a list of all lists in that开发者_如何学运维 subsite. This code returns the top level "http://myspserver" lists.
How do I get only the lists from /mysubsite?string webUrl = "http://myspserver/mysubsite";
using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb()) //Open SP Web
{
SPListCollection collList = oWebsite.Lists; //Open Lists
foreach (SPList oList in SPContext.Current.Web.Lists)
//For Each List Execute this
{
....
}
}
You should iterate over collList
, not SPContext.Current.Web.Lists
.
foreach (SPList oList in collList)
{
}
SPContext.Current.Web.Lists
will get the site you are currently in. Presumably, this is http://myspserver
when you run your code.
Also, note that your code leaks - you do not dispose of the SPSite object. It should look like:
using(SPSite site = new SPSite(webUrl))
using(SPWeb oWebsite = site.OpenWeb())
{
}
You create the SPListCollection
object, but there you use the SPContext.Current.Web.Lists
in your foreach
, correct your code like this and things should be fine:
string webUrl = "http://myspserver/mysubsite";
using (SPWeb oWebsite = new SPSite(webUrl).OpenWeb()) //Open SP Web
{
SPListCollection collList = oWebsite.Lists; //Open Lists
foreach (SPList oList in collList)
//For Each List Execute this
{
....
}
}
精彩评论