【问题标题】:SharePoint Subsite - Iterate over listsSharePoint 子网站 - 遍历列表
【发布时间】:2011-02-16 15:46:36
【问题描述】:

我有一个 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
    {
        ....
    }
}

【问题讨论】:

标签: c# sharepoint-2007


【解决方案1】:

您应该迭代 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())
{
}

【讨论】:

  • 我还在努力理解内存泄漏的概念,有没有什么信息可以帮助我理解泄漏。
  • @josephs8 - 我明天会回复你,但应该很容易找到好的资源。
  • Disposing 更关心释放非托管资源(在某些情况下可能是更有限的资源),而不是托管内存。
【解决方案2】:

您创建了SPListCollection 对象,但您在foreach 中使用了SPContext.Current.Web.Lists,像这样更正您的代码,一切都会好起来的:

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

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 1970-01-01
    • 2015-09-21
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 2017-08-09
    相关资源
    最近更新 更多