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:
1 |
<Feature xmlns=<span style="color: #006080">"http://schemas.microsoft.com/sharepoint/"</span>...> |
1 |
<Properties xmlns=<span style="color: #006080">"http://schemas.microsoft.com/sharepoint/"</span>> |
1 |
<!-- The name of the timer job (will appear <span style="color: #0000ff">in</span> Central Admin). --> |
1 |
<Property Key=<span style="color: #006080">"JobTitle"</span> Value=<span style="color: #006080">"Your Timer Job Name Here"</span>/> |
1 |
<!-- Connection String that the TimerJob will use --> |
1 |
<Property Key=<span style="color: #006080">"ConnString"</span> Value=<span style="color: #006080">"ConnectionString..."</span>/> |
1 |
<!-- The schedule to run the job <span style="color: #0000ff">in</span> (24 hour format) --> |
1 |
<Property Key=<span style="color: #006080">"Schedule"</span> Value=<span style="color: #006080">"daily at 02:00:00"</span>/> |
1 |
</Properties> |
1 |
</Feature> |
Then, during the FeatureActivated event, I passed those values into the Properties bag on the Timer Job class:
1 |
<span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> FeatureActivated(SPFeatureReceiverProperties properties) |
1 |
{ |
1 |
1 |
<span style="color: #008000">// Get all the properties from the feature.xml file</span> |
1 |
<span style="color: #0000ff">string</span> jobTitle = properties.Feature.Properties[<span style="color: #006080">"JobTitle"</span>].Value; |
1 |
<span style="color: #0000ff">string</span> connString = properties.Feature.Properties[<span style="color: #006080">"ConnString"</span>].Value; |
1 |
<span style="color: #0000ff">string</span> dailySchedule = properties.Feature.Properties[<span style="color: #006080">"Schedule"</span>].Value; |
1 |
1 |
<span style="color: #008000">// Create the job.</span> |
1 |
CustomTimerJob customTimerJob = <span style="color: #0000ff">new</span> CustomTimerJob((SPWebApplication)properties.Feature.Parent, jobTitle); |
1 |
1 |
<span style="color: #008000">// Set the properties for the job to run properly</span> |
1 |
customTimerJob.Properties.Add(<span style="color: #006080">"ConnString"</span>, connString); |
1 |
customTimerJob.Properties.Add(<span style="color: #006080">"Schedule"</span>, dailySchedule); |
1 |
1 |
<span style="color: #008000">// Set the schedule</span> |
1 |
SPSchedule mainSchedule = SPSchedule.FromString(dailySchedule); |
1 |
customTimerJob.Schedule = mainSchedule; |
1 |
1 |
<span style="color: #008000">// Activate the schedule</span> |
1 |
customTimerJob.Update(); |
1 |
1 |
} |
Then, in the Timer Job’s Execute method, grabbed those settings and used them:
1 |
<span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Execute(Guid targetInstanceId) |
1 |
{ |
1 |
<span style="color: #008000">// Set up configuration values</span> |
1 |
<span style="color: #0000ff">string</span> connString = <span style="color: #0000ff">this</span>.Properties[<span style="color: #006080">"ConnString"</span>] <span style="color: #0000ff">as</span> <span style="color: #0000ff">string</span>; |
1 |
1 |
... |
1 |
} |
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
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?
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
Pingback: Rescheduling and running MOSS Timer Job « DotNetRaj