2011-04-09 30 views
3
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總是返回「上下文爲NULL」。並且sFeatureNull總是返回「功能爲空」也是。有沒有辦法獲得不空值的SPContext.Currentproperties.Feature功能事件具有「空」對象

另一種方法FeatureActivated返回Context是NULL特徵是行。 WTF?

回答

1

可能properties.Feature在方法FeatureInstalled是一個錯誤。我曾嘗試下面的代碼和它的作品對我來說:

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

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

此方法的返回特點是OK

請避免在方法使用properties.Feature FeatureInstalledFeatureUninstalling

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命令,然後通常觸發計時器工作來完成工作。此時沒有HTTP請求,所以SPContext.Current返回null。

+0

謝謝!什麼是**屬性。功能**? – NieAR 2011-04-09 09:37:20

+0

你可以嘗試調試事件http://blogs.msdn.com/b/chriskeyser/archive/2010/07/26/debugging-a-featureinstalled-event.aspx – djeeg 2011-04-09 09:43:08

+0

這似乎與你的問題類似http: http://suguk.org/forums/thread/16667.aspx – djeeg 2011-04-09 09:43:34

1

我想你需要獲得包含此功能,所以請儘量使用網絡

(properties.Feature.Parent as SPWeb) 

它正常工作與我

注意:儘量施展它的SPSite如果功能範圍網站

相關問題