Using Feature XML Files to Store Timer Job Configuration Settings

While working on a Timer Job project, I needed a flexible way to store one-time configuration settings. I was using a SharePoint Feature and FeatureReceiver to install and activate the Timer Job, so I decided to use the Feature.XML file as a repository for the initial configuration settings Job.

I used the Properties node of the feature.xml file to store the settings:

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"...>
  <Properties xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- The name of the timer job (will appear in Central Admin). -->
    <Property Key="JobTitle" Value="Your Timer Job Name Here"/>
    <!-- Connection String that the TimerJob will use -->
    <Property Key="ConnString" Value="ConnectionString..."/>
    <!-- The schedule to run the job in (24 hour format) -->
    <Property Key="Schedule" Value="daily at 02:00:00"/>
  </Properties>
</Feature>

Then, during the FeatureActivated event, I passed those values into the Properties bag on the Timer Job class:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

    // Get all the properties from the feature.xml file
    string jobTitle = properties.Feature.Properties["JobTitle"].Value;
    string connString = properties.Feature.Properties["ConnString"].Value;
    string dailySchedule = properties.Feature.Properties["Schedule"].Value;

    // Create the job.
    CustomTimerJob customTimerJob = new CustomTimerJob((SPWebApplication)properties.Feature.Parent, jobTitle);

    // Set the properties for the job to run properly
    customTimerJob.Properties.Add("ConnString", connString);
    customTimerJob.Properties.Add("Schedule", dailySchedule);

    // Set the schedule
    SPSchedule mainSchedule = SPSchedule.FromString(dailySchedule);
    customTimerJob.Schedule = mainSchedule;

    // Activate the schedule
    customTimerJob.Update();

}

Then, in the Timer Job’s Execute method, grabbed those settings and used them:

public override void Execute(Guid targetInstanceId)
{
    // Set up configuration values
    string connString = this.Properties["ConnString"] as string;

    ...
}

I didn’t have a need to make run-time changes to the configuration settings, so this worked out really well. To change the defaults and apply new settings, the process was as simple as:

  • Deactivate the feature (uninstalls the Timer Job)
  • Change the feature.xml file
  • Re-activate the feature
3 comments on “Using Feature XML Files to Store Timer Job Configuration Settings
  1. Good idea – but I have a problem:

    Just deactivate/reactive the TimerJob over the Feature does not reload the new content from the feature.xml on the filesystem. I have to make a “iisreset” to provoke a reload of the filesystem feature.xml settings.

    Have I done something wrong?

  2. Hey Adam,
    Superb article. I was struggling a bit to find a better way of providing configuration values that can be read at the time of Feature Activation. Your article solved this in minutes.
    Thanks again.
    -Yogi

  3. Pingback: Rescheduling and running MOSS Timer Job « DotNetRaj

Comments are closed.