SP2013 TimerJob 构造函数 SPServer 参数

SP2013 TimerJob constructor SPServer parameter

我有一个计时器作业,我正在尝试 运行 特定服务器上的这个计时器作业,下面是我试图用来比较服务器名称和在 FeatureActivated 事件上创建计时器作业实例的代码。我不知道该怎么做。如果我完全错了,请帮助我并纠正我。

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
        }
    }

首先,你需要使用SPServerJobDefinition class.

其次,从 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
}