March 3, 2021
Hot Topics:

Running Timed Jobs within SharePoint

  • By Gustavo Velez
  • Send Email »
  • More Articles »

Using the Command Line

The "stsadm" Administrators command-line application can be used to control certain Timed Jobs functionality. Unfortunately, there are no default commands to install or start/stop Timed Jobs, but there is a command to run all Jobs immediately and commands to reschedule a number of Jobs.

The command "execadmsvcjobs" executes all the configured Timed Jobs present in the system immediately, as an alternative to waiting for the expiration of the time frame. This command does not require any parameters; the syntax is:

stsadm -o execadmsvcjobs

Bear in mind that, as all Jobs commence at the same time, it may present a burden on SharePoint servers. Also, if the Windows SharePoint Services Timer service has been stopped for any reason, the aforementioned command may be used to manually run the Jobs in the Portal.

There are also SharePoint components that use a Timed Job and can be controlled using the command-line tool. For example, the commands "setpolicyschedule", "setcontentdeploymentjobschedule", and "setsitedirectoryscanschedule" are components that support the Timed Job infrastructure. Each of the commands accepts a "-schedule" parameter (named "recurrence string" or "schedule" in the help function of stsadm) that is a free string of the form:

"[Time frame] [Interval] between [value] and [value]"

For example:

"every 5 minutes between 0 and 59"
"hourly between 0 and 59"
"daily at 15:00:00"
"weekly between Fri 22:00 and Sun 06:00:00"
"yearly at Jan 1 15:00:00"

The syntax looks like this:

stsadm -o setsitedirectoryscanschedule
       -schedule "daily at 03:00:00"

Using the Object Model

The Object Model of SharePoint also enables developers to manage Timed Jobs. The classes SPJobDefinitionCollection and SPJobDefinition from the NameSpace Microsoft.SharePoint.Administration contain the methods and properties necessary to operate Timed Jobs.

The class SPJobDefinitionCollection contains the entire Timed Jobs selection of a SharePoint Web application, each of the type SPJobDefinition. The SPJobDefinition class contains the properties that define a Job (Name, Title, Version, and in which Server and WebApplication it is working, and so forth) and the methods to process it (Execute, Delete, Update). A special property, "Schedule", of the type "SPJobDefinition.Schedule", sees to the time frame for the Job.

To enumerate all the Jobs in an application, the following routine can be used in a command-line application to show the title, last run time, and type of each Job:

static void GetAllJobs()
   SPSite mySite = new SPSite("http://ServerName");

   foreach (SPJobDefinition oneJob in
      Console.WriteLine(oneJob.Title + " -" +
         oneJob.LastRunTime.ToLongTimeString() + " - " +
         oneJob.Schedule.GetType().ToString().Remove(0, 21));

Note that the string "Microsoft.SharePoint" has been stripped from the schedule type.

Click here for a larger image.

Figure 3: Output from Timed Jobs for the application href="http://ServerName", together with the Central Administration Page showing the same results

The routine loops through the Job definitions of the application, displaying various properties of each found Job. It is necessary to set a reference to Windows SharePoint Services (Microsoft.SharePoint.dll) and "using" directives to Microsoft.SharePoint and Microsoft.SharePoint.Administration.

The next routine adds a new Job Definition to the application:

static void CreateJob()
   SPServer myServer = SPServer.Local;
   SPSite mySite = new SPSite("http://ServerName");
   SPWebApplication myApplication = mySite.WebApplication;
   SPJobDefinitionCollection myJobsColl = myApplication.JobDefinitions;

   SPJobDefinition myJob = new SPJobDefinition("FakeTestJob",
      myApplication, myServer, SPJobLockType.Job);
   myJob.Title = "Fake Test Job";

   SPDailySchedule myDailySchedule = new SPDailySchedule();
   myDailySchedule.BeginHour = 3;
   myDailySchedule.BeginMinute = 0;
   myDailySchedule.BeginSecond = 0;
   myJob.Schedule = myDailySchedule;


   Console.WriteLine("Job Installed");

Page 2 of 5

This article was originally published on August 14, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date