SharePoint: Timer Job Bits and Pieces


Having developed a custom Timer Job, and deployed it to the farm, it can be useful during development and testing to to be able to execute it immediately and also later to be able to change the execution schedule.

Both of these tasks can be done using STSADM and or the Central Administration  UI, however you can also do this using code, and combined with a custom administration page it can be pretty useful.

Execute a Custom Timer Job.

var ej = from SPJobDefinition job in webApp.JobDefinitions
			   where job.Name == "CUSTOM JOB DEFINITION NAME"
			   select job;
if (ej.Count() > 0)
	ej.First().Execute(Guid.Empty);

Change or Update a Timer Job Schedule.

var ej = from SPJobDefinition job in webApp.JobDefinitions
			   where job.Name == "CUSTOM JOB DEFINITION NAME"
			   select job;
if (ej.Count() < 1) return;

var jd = ej.First();
var newSched = new SPMinuteSchedule
						{
							BeginSecond = 0,
							EndSecond = 59,
							Interval = 30
						};
jd.Schedule = newSched;
jd.Update();

Timer Job Schedule Options:

When setting a schedule for your Timer Job, you have the following options;

SPMinuteSchedule:

Use this schedule to set processing intervals of between 1 and 59 minutes, setting the Interval property to values outside the 1 – 59 range will not throw an Exception but the interval and Timer Job will be ignored by the Windows SharePoint Services Timer (SPTimerV3) service.

var newSched = new SPMinuteSchedule
						{
							BeginSecond = 0,
							EndSecond = 59,
							Interval = 15
						};

Every 15 minutes between 0 and 59 seconds

SPHourlySchedule:

Use this schedule to set hourly processing intervals.

var newSched = new SPHourlySchedule
						{
							BeginMinute = 0,
							EndMinute = 59
						};

Every hour between 0 and 59 minutes

SPDailySchedule:

Use this schedule to set daily processing intervals.

var newSched = new SPDailySchedule
				{
					BeginHour = 0,
					BeginMinute = 0,
					BeginSecond = 0,
					EndHour = 23,
					EndMinute = 59,
					EndSecond = 59,
				};

Every day between 0 hours, 0 minutes, 0 seconds and 23 hours, 59 minutes and 59 seconds

Note:

When executing the Time Job manually using the code shown, the Time Job executes under the W3WP process (if called from SharePoint page code or the process under which the .Execute() method is called) rather than the usual Windows SharePoint Services Timer OWSTIMER process.

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s