Export (0) Print
Expand All
Expand Minimize
9 out of 12 rated this helpful - Rate this topic

Creating, Deploying, and Debugging Custom Timer Jobs in Windows SharePoint Services 3.0

Windows SharePoint Services 3

Summary:  Learn how to harness the power of custom timer jobs in Windows SharePoint Services 3.0.

Visual How To

Applies to:  Windows SharePoint Services 3.0, Microsoft Office SharePoint Server 2007, Microsoft Visual Studio 2005

MVP Icon  Andrew Connell, Critical Path Training, LLC

March 2008

Overview

Windows SharePoint Services 3.0 lets you create custom jobs that are executed at set intervals. These jobs, known as timer jobs, are similar to those tasks that you can create in any version of Windows by using the Task Scheduler application. This capability is useful for scheduled process jobs because you keep everything in Windows SharePoint Services rather than create a console .exe file that is configured to run at set intervals by using the Windows Task Scheduler. The major benefits to using the Windows SharePoint Services timer service compared to using Windows Task Scheduler is that the timer service knows the topology of the Office SharePoint Server farm, and that you can load balance the jobs across all the servers in the farm or tie them to specific servers that are running particular services. This Microsoft Office Visual How To demonstrates how to create a job that requests the home page of each site collection in a Web application to force just-in-time (JIT) compilation and to speed up the first request.

Code It

Timer jobs are represented as Microsoft.SharePoint.Administration.SPJobDefinition objects. To create one, create a new class that inherits from SPJobDefinition. This new class minimally needs one constructor to create the job and a method that overrides the Execute method. This is the method that is called when the job is executed by Windows SharePoint Services.

public SharePointWarmupJob (SPWebApplication webApp)
  : base(JOB_NAME, webApp, null, SPJobLockType.ContentDatabase) {
    this.Title = JOB_NAME;
}
public override void Execute (Guid targetInstanceId) {
  if (this.WebApplication.Sites.Count > 0)
    WarmUpSiteCollection(this.WebApplication.Sites[0]);
}
private void WarmUpSiteCollection (SPSite siteCollecion) {
  WebRequest request = WebRequest.Create(siteCollecion.Url);
  request.Credentials = CredentialCache.DefaultCredentials;
  request.Method = "GET";
  WebResponse response = request.GetResponse();
  response.Close();
}

Installing Custom Timer Jobs

After you create a timer job, the next step is to install it. Unfortunately, you cannot do this through the provided browser-based user interface; however, you can use the Windows SharePoint Services API. Developers can create custom applications, use PowerShell, or create custom STSADM commands to install the custom timer job. However, another technique is to use a Windows SharePoint Services 3.0 Feature to install and uninstall the custom timer job. Use a Feature receiver to install the feature as shown in the following example.

public override void FeatureActivated (SPFeatureReceiverProperties props) {
  SPWebApplication webApp = props.Feature.Parent as SPWebApplication;
  if (webApp == null)
    throw new SPException("Error obtaining reference to Web application.");

  // Ensure the job is not already registered.
  foreach (SPJobDefinition job in webApp.JobDefinitions)
    if (job.Name == JOB_NAME) job.Delete();

  // Install job.
  SharePointWarmupJob warmupJob = new SharePointWarmupJob(webApplication);

  // Schedule the job to run every minute all the time.
  SPMinuteSchedule schedule = new SPMinuteSchedule();
  schedule.BeginSecond = 0;
  schedule.EndSecond = 59;
  schedule.Interval = 1;
  warmupJob.Schedule = schedule;

  // Save changes.
  warmupJob.Update();
}
Read It

Creating custom timer jobs in Windows SharePoint Services 3.0 can be useful when a project requires a scheduled process to be executed. Timer jobs are the preferred way to create these scheduled tasks instead of creating a console application scheduled with Windows Task Scheduler.

One thing to remember when you are using site-scoped or site collection–scoped Features to install the timer jobs: The account that does the scheduling must have write permissions to the configuration database of the server farm. Usually, the identity of the application pool that is hosting the SharePoint site does not have permissions on the configuration database. This is the account that is used when you activate the Feature through the browser interface. However, if you activate the Feature by using STSADM, the identity of the interactive user is used. This enables someone with greater user rights to install the timer job.

See It

Creating, Deploying and Debugging Timer Jobs

Watch the Video

Length: 19:34 | Size: 19.5 MB | Type: WMV

Explore It
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.