2017-05-29 6 views
0

我有一個計時器作業,我試圖在特定服務器上運行此計時器作業,下面是我試圖用來比較服務器名稱和在FeatureActivated事件上創建計時器作業實例的代碼。我不知道如何做到這一點。如果我完全錯了,請幫助我並糾正我。SP2013 TimerJob構造函數SPServer參數

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
    { 
     // Get an instance of the SharePoint farm. 
     //SPFarm farm = SPFarm.Local; 

     SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; 

     // Remove job if it exists. 
     DeleteJobAndSettings(webApp); 

     var serverName = SPServer.Local.DisplayName; 
     if (string.Equals("sp2013", serverName, StringComparison.OrdinalIgnoreCase)) 
     { 
      // Create the job. 
      MyReportNew job = new MyReportNew(webApp, SPServer.Local); 

      //Other code 
     } 
    } 

回答

1

首先,您需要使用SPServerJobDefinition類。

其次,從SPFarm.Local.Servers集合中檢索SPServer對象。

例如:

public class CustomJob : SPServerJobDefinition 
{ 
    public CustomJob() 
     : base() 
    { 
    } 

    public CustomJob(string jobName, SPServer server) 
     : base(jobName, server) 
    { 
     this.Title = jobName; 
    } 

    public override void Execute(SPJobState state) 
    { 
     // do stuff 
    } 
} 

public override void FeatureActivated(SPFeatureReceiverProperties properties) 
{ 
    // Get an instance of the SharePoint farm. 
    SPFarm farm = SPFarm.Local; 

    // Remove job if it exists. 
    DeleteJobAndSettings(webApp); 

    // Create the job. 
    MyReportNew job = new MyReportNew("MyJobName", farm.Servers["sp2013"]); 

    //Other code 
} 
+0

謝謝@tinamou,我沒有的我在做什麼想法,感謝清理了我的疑慮。我會試着讓你知道,如果它的工作原理.. –