在事件接收器中获取SPContext

Dr.*_*izz 7 c# sharepoint spcontext event-receiver

我创建了一个事件接收器,但问题是我无法获得对SPContext:SPContext.Currentreturn 的引用null.我需要它来添加一些列表到网站.有谁知道我怎么能得到它?

此外,我尝试在事件接收器中放置断点,但FeatureActivates由于某种原因从未触发.在部署之后立即激活列表时使用的正确事件是什么?

小智 15

你无法进​​入SPContext内部处理程序 - 这是设计的.您应该使用作为参数传递给处理程序的事件属性来获取对当前Web,列表项等的引用.例如,在功能激活处理程序中,您可以这样做:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;  
    //Some code with web
}
Run Code Online (Sandbox Code Playgroud)

如果Feature Scope是Site,那么

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
        SPSite site = properties.Feature.Parent as SPSite;  
        //Some code with web
}
Run Code Online (Sandbox Code Playgroud)


Gur*_*Kay 5

我认为该功能的范围很重要.如果您在站点范围中部署了该功能,则可以使用以下代码行获取Web:

SPWeb web = (properties.Feature.Parent as SPSite).OpenWeb();
Run Code Online (Sandbox Code Playgroud)