SharePoint 子网站 - 迭代列表
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该迭代
collList
,而不是SPContext.Current.Web.Lists
。SPContext.Current.Web.Lists
将获取您当前所在的站点。当您运行代码时,大概是http://myspserver
。另外,请注意您的代码泄漏 - 您没有处置 SPSite 对象。它应该看起来像:
You should iterate over
collList
, notSPContext.Current.Web.Lists
.SPContext.Current.Web.Lists
will get the site you are currently in. Presumably, this ishttp://myspserver
when you run your code.Also, note that your code leaks - you do not dispose of the SPSite object. It should look like:
您创建了
SPListCollection
对象,但在foreach
中使用了SPContext.Current.Web.Lists
,更正了您的代码,如下所示应该没问题:You create the
SPListCollection
object, but there you use theSPContext.Current.Web.Lists
in yourforeach
, correct your code like this and things should be fine: