SharePoint子网站 - 迭代列表

时间:2011-02-16 15:46:36

标签: c# sharepoint-2007

我有一个SharePoint网站。我正在尝试打开子网站并获取该子网站中所有列表的列表。此代码返回顶级“http:// myspserver”列表 我如何只获取/ 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
    {
        ....
    }
}

2 个答案:

答案 0 :(得分:4)

您应该遍历collList,而不是SPContext.Current.Web.Lists

foreach (SPList oList in collList)
{
}

SPContext.Current.Web.Lists将获取您当前所在的网站。据推测,当您运行代码时,这是http://myspserver

另外,请注意您的代码泄漏 - 您不会丢弃SPSite对象。它应该看起来像:

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

答案 1 :(得分:2)

您创建了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
    {
       ....
    }
}