Thursday, July 28, 2011

Updating Timer Job Schedule

Some time we need to build and deploy a timer job with specific schedule say hourly or daily.
But for testing with the business or on your own , you don't want to wait for a day to see the change

Here is a simple console app code which you can use to change timer schedule for testing for any other reason

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace UpdateTimerJob
{
  class Program
  {       
   
static void Main(string[] args)
    {
      updateTimer();
    }

static void updateTimer()

{
  using (SPSite site = new SPSite("http://sp2010/sites/timer/"))
  {    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
    {      if (job.Title == "WorkflowEscalation")
      {
        SPMinuteSchedule schedule =
new SPMinuteSchedule();
        schedule.BeginSecond = 0;
        schedule.EndSecond = 59;
        schedule.Interval = 5;
        job.Schedule = schedule;
        job.Update();


        Console.WriteLine("START UPDATING... ");
       }
     
Console.WriteLine("JOB: " + job.Title);
    }
  }
}
} }


Note: this need to run on the server; you can further modify this app to read site, title & schedule, then update accordingly

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