SharePoint 子网站 - 迭代列表

发布于 2024-10-17 11:07:14 字数 435 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2024-10-24 11:07:14

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

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())
{
}
怎樣才叫好 2024-10-24 11:07:14

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

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
    {
       ....
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文