我创建了一个事件接收器,但问题是我无法获得对 SPContext
的引用:SPContext.Current
返回 null
。我需要它来向站点添加一些列表。有谁知道我怎么能得到它?
我还尝试在事件接收器中放置断点,但 FeatureActivates
由于某种原因从未触发。在部署后立即激活列表时使用的正确事件是什么?
最佳答案
您无法在处理程序中获取 SPContext
- 这是设计使然。您应该使用作为参数传递给处理程序的事件属性来获取对当前网站、列表项等的引用。
例如,在功能激活的处理程序中,您可以这样做:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
//Some code with web
}
如果特征范围是站点则
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
//Some code with web
}
关于c# - 在事件接收器中获取 SPContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9468195/