SharePoint sub-site - iterate over lists

I have a SharePoint site. I am trying to open a child node and get a list of all the lists on this child site. This code returns the top level lists "http: // myspserver".
How to get only 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
    {
        ....
    }
}
+3
source share
2 answers

You have to iterate collList, not SPContext.Current.Web.Lists.

foreach (SPList oList in collList)
{
}

SPContext.Current.Web.Listswill receive the site you are currently on. Presumably this is http://myspserverwhen you run your code.

Also note that your code is leaking - you are not deleting the SPSite object. It should look like this:

using(SPSite site = new SPSite(webUrl))
using(SPWeb oWebsite = site.OpenWeb())
{
}
+4
source

SPListCollection, SPContext.Current.Web.Lists foreach, , , :

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
    {
       ....
    }
}
+2

Source: https://habr.com/ru/post/1792563/


All Articles