機能受信機で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); } }
私は何をしていますか?
解決
how about;
warning: your feature needs to be scoped as web for it to work obviously ;)
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);
}
not using feature:
if not then use the spcontext to get the root web
SPContext.Current.Site.RootWeb
or - for the current web
SPContext.Current.Web
or - for a specific web url
SPContext.Current.Site.OpenWeb("Website_URL"))
to use above in feature you would need to use the properties:
use the properties to get the site to get the rootWeb
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.RootWeb)
{
}
or - for the current web
SPWeb web = properties.Feature.Parent as SPWeb;
or - for a specific web url
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.OpenWeb("Website_URL"))
{
}
他のヒント
You're doing right. Just the scope of the feature must be web. The "Parent" attribute is always the object of the feature scope.
You're also disposing the object with using.
The only problem with SPContext is that you can't activate the feature from PowerShell as there is no http context.