I have a timer job and I am trying to run this timer job on specific server, below is the code I am trying to use to compare server name and creating instance of timer job on FeatureActivated event. I have no idea how to do this. Please help me and correct me if I am doing it totally wrong.
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
}
}
Firstly, you need to use SPServerJobDefinition class.
Secondly, retrieve SPServer object from SPFarm.Local.Servers collection.
For example: