在特征接收器中获取SPWeb
-
10-12-2019 - |
题
在测试我的解决方案时,我会在功能激活时收到错误:
检测用于以前关闭的SPWeb对象的spRequest。完成所有对象,请关闭SPWeb对象,但不是之前。
这是我得到spweb的方式:
.public override void FeatureActivated(SPFeatureReceiverProperties properties) { using (SPWeb web = properties.Feature.Parent as SPWeb) { ClassOfMine.doYourStuff(web); } }
我做错了什么?
解决方案
怎么样;
警告:您的功能需要被视为Web,因为它明显地工作;)
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// No need to dispose the web istance, as indicated in the "Do not dispose" guidance
SPWeb web = (SPWeb) properties.Feature.Parent; // added semicolon
ClassOfMine.doYourStuff(web);
}
.
不使用功能:
如果不是,请使用spcontext获取root web
SPContext.Current.Site.RootWeb
.
或 - 对于当前的web
SPContext.Current.Web
.
或 - 对于特定的Web URL
SPContext.Current.Site.OpenWeb("Website_URL"))
.
在功能中使用您需要使用的属性:
使用属性获取站点以获取rootweb
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.RootWeb)
{
}
.
或 - 对于当前的web
SPWeb web = properties.Feature.Parent as SPWeb;
.
或 - 对于特定的Web URL
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.OpenWeb("Website_URL"))
{
}
. 其他提示
你做得对。只是该功能的范围必须是Web。“父”属性始终是特征范围的对象。
您还使用使用。
spcontext的唯一问题是您无法激活PowerShell的功能,因为没有HTTP上下文。