功能事件具有“空”对象

时间:2011-04-09 07:26:22

标签: sharepoint sharepoint-2010

public class Feature1EventReceiver : SPFeatureReceiver
{

    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {

        string sContextNull = (SPContext.Current == null) ? "Context is NULL" : "Context is OK";
        string sFeatureNull = (properties.Feature == null) ? "Feature is NULL" : "Feature is OK";

        // Some code here
        ...
        ...
    {
}

该功能已成功安装(日志中没有错误)。我的问题是 sContextNull 总是返回“Context is NULL”。并且 sFeatureNull 也始终返回“功能为空”。有没有办法获得 SPContext.Current properties.Feature 的空值?

另一种方法 FeatureActivated 返回上下文为空功能正常。 WTF?

3 个答案:

答案 0 :(得分:2)

SPContext.Current

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.current.aspx

Gets the context of the current HTTP request in Microsoft SharePoint Foundation.

当一个功能安装到服务器场时,

SPFeatureReceiver.FeatureInstalled正在执行,这是通过stsadm或powershell的deploy / install命令完成的,然后通常会触发计时器作业来完成工作。此时没有HTTP请求,因此SPContext.Current返回null。

答案 1 :(得分:1)

方法 FeatureInstalled 中的 properties.Feature 可能是一个错误。我已经尝试了下一个代码,它对我有用:

public class Feature1EventReceiver : SPFeatureReceiver
{    
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {    
        string sFeatureNull = (properties.Feature == null) ? "Feature is NULL" : "Feature is OK";

        // Some code here
        ...
        ...
    {
}

此方法返回功能正常

请避免在方法 FeatureInstalled FeatureUninstalling !!!

中使用 properties.Feature

答案 2 :(得分:1)

我认为您需要获取包含此功能的网页,因此请尝试使用

(properties.Feature.Parent as SPWeb)

它适用于我

注意:如果要素范围是站点

,请尝试将其强制转换为SPSite