Wednesday, July 27, 2011

Timer Job With Trick

Timer Job our good old friend, for further details or quick start take a look at this http://technet.microsoft.com/en-us/library/cc678870(office.12).aspx 

A little know trick I have implement in my timer job so that it install only on Web front end or Single server (ex. DEV box, usually all in one box).

const string JOB_DEFINITION_NAME = "EscalationTimerJob";

public override void FeatureActivated(SPFeatureReceiverProperties properties)
 {
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {                     
   #region ONLY WebFrontEnd/SingleServer
   SPSite site = properties.Feature.Parent as SPSite;
   SPWebApplication webApp = site.WebApplication;

   //make sure job isn't already registered
   if (webApp == null)
   {
    throw new SPException("Error obtaining reference to Web application.");
   }
   foreach (SPJobDefinition job in webApp.JobDefinitions)
   {    if (job.Name == JOB_DEFINITION_NAME)
       job.Delete();
   }
   foreach (SPServer server in SPFarm.Local.Servers)
   {    if (server.Role == SPServerRole.SingleServer || server.Role == SPServerRole.WebFrontEnd)
 {
  //Install the Job                        
  WorkflowEscalation taskJob = new WorkflowEscalation(JOB_DEFINITION_NAME, webApp, server, SPJobLockType.Job);
  SPHourlySchedule schedule = new SPHourlySchedule(); //Every Hour
  schedule.BeginMinute = 0;
  schedule.EndMinute = 59;
  taskJob.Schedule = schedule;
  taskJob.Update();
  }
}
#endregion
});
}


Note:
1. Added RunWithElevatedPrivileges, so that job definition will be create when activated even without full access. Feature activation runs under the application pool account. So it needs to be a farm administrator which will be mitigate by implementing RunWithElevatedPrivileges

2. Also make sure you also implemented below in your main execute() file
public WorkflowEscalation(string jobName, SPWebApplication webApp, SPServer server, SPJobLockType targetType): base(jobName, webApp, server, targetType)
 {
  this.Title = jobName;
 }

Usual code for installing in on all the servers:
const string JOB_DEFINITION_NAME = "EscalationTimerJob";
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(
delegate()
{
  #region For Multiple Servers
  SPSite site = properties.Feature.Parent as SPSite;
  //make sure job isn't already registered
  if (site.WebApplication == null)
  {
   
throw new SPException("Error obtaining reference to Web application.");
  }

  foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)

  {
   
if (job.Name == JOB_DEFINITION_NAME)
      job.Delete();
  }

 
//Install the Job                    
  WorkflowEscalation taskJob = new WorkflowEscalation(JOB_DEFINITION_NAME, site.WebApplication);
  SPHourlySchedule schedule = new SPHourlySchedule(); //Every Hour
  schedule.BeginMinute = 0;
  schedule.EndMinute = 59;
  taskJob.Schedule = schedule;
  taskJob.Update();

  #endregion                                     
  });
}

No comments: