0

我有一个计时器作业,我正在尝试在特定服务器上运行此计时器作业,下面是我试图用来比较服务器名称和在 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
        }
    }
4

1 回答 1

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
}
于 2017-05-29T19:03:21.993 回答