2011-09-16 33 views
1

我創建了自定義計時器作業,並通過SharePoint中的功能事件實現它。「激活功能」步驟錯誤:對象引用未設置爲對象的實例

當我試圖激活它,它顯示此特定錯誤:

Object reference not set to an instance of an object. at TimerJobExample.MyFeatureReceiver.FeatureActivated(SPFeatureReceiverProperties properties) in C:\Documents and Settings\admin-shuklag.INFRADEV\My Documents\Visual Studio 2008\Projects\TimerJobExample\TimerJobExample\MyFeatureReceiver.cs:line 22 
    at Microsoft.SharePoint.SPFeature.DoActivationCallout(Boolean fActivate, Boolean fForce) 
    at Microsoft.SharePoint.SPFeature.Activate(SPSite siteParent, SPWeb webParent, SPFeaturePropertyCollection props, Boolean fForce) 
    at Microsoft.SharePoint.SPFeatureCollection.AddInternal(Guid featureId, SPFeaturePropertyCollection properties, Boolean force, Boolean fMarkOnly) 
    at Microsoft.SharePoint.SPFeatureCollection.Add(Guid featureId) 
    at Microsoft.SharePoint.WebControls.FeatureActivator.BtnActivateFeature_Click(Object objSender, EventArgs evtargs) 
    at System.Web.UI.WebControls.Button.OnClick(EventArgs e) 
    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) 
    at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) 
    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) 
    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) 
    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

錯誤行高亮顯示下方

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
    { 

     SPSite site = properties.Feature.Parent as SPSite; 

     // make sure the job isn't already registered 


//error line 
     **foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)** 

     { 

      if (job.Name == List_JOB_NAME) 

       job.Delete(); 

     } 



     SampleTimer listLoggerJob = new SampleTimer(List_JOB_NAME, site.WebApplication); 

     SPMinuteSchedule schedule = new SPMinuteSchedule(); 

     schedule.BeginSecond = 0; 

     schedule.EndSecond = 59; 

     schedule.Interval = 5; 

     listLoggerJob.Schedule = schedule; 

     listLoggerJob.Update(); 

    } 

可能是什麼問題呢?

+0

你可以發佈'FeatureActivated'方法代碼嗎? –

+0

在MyCustomJob構造函數中發生了什麼? –

回答

1

問題在於功能的範圍。它已設置爲Web,並且要在此級別激活功能,您需要一個管理員帳戶或具有提升特權的帳戶。

You can either add the application pool id to farm admin accounts or run the code with elevated rights.

5

根據您的功能接收器,我可以看到只有兩種可能性:

  1. 仔細檢查feature.xml中scope屬性設置爲站點。如果它設置爲Web,則在嘗試使用site變量時將會遇到Object Reference not set異常。
  2. 正如賈尼斯所說,看看MyCustomJob的構造函數。確保所有變量都已正確初始化。
相關問題