DCSIMG
Storing Configuration Settings For Timer Jobs - itaysk

Storing Configuration Settings For Timer Jobs

Posted Friday, March 23, 2012 3:58 PM by Itay Shakury

When you develop a custom timer job, there is often a need to store configuration settings.
The best way to do this is using the Hierarchical Object Store in SharePoint, by creating an SPPerststedObject. There are plenty of articles out there that tell you to how to do this.
For more info on this subject, see this:
http://msdn.microsoft.com/en-us/library/cc406686(office.12).aspx
http://msdn.microsoft.com/en-us/library/hh528519.aspx

So it’s called "hierarchical” object store, because you have to attach the settings to a certain object in the SharePoint object model, and that object must support this by inheriting from SPPersistedObject. Most of those articles shows you how to attach settings to SPFarm, SPWebApplication, or SPSiteCollection objects, but as a matter of fact, the SPJobDefinition class is also a SPPersistedObject!

This means that you can attach settings to the job object directly, and not leave them hanging at the farm or a higher level. This has two benefits:

  1. It’s more organized to keep timer job related settings at the timer job level (and not web application level for example)
  2. When you delete the timer job, the settings are deleted as well

Here’s a sample:

   1: SPWebApplication wa = SPWebApplication.Lookup(new Uri("http://MySP"));
   2: SPJobDefinition job = wa.JobDefinitions.First(j => j.Name == "MyTimerJob");
   3:  
   4: //Store the settings
   5: MyJobSettings settings = new MyJobSettings("settings", job); //here we set job as the parent
   6: settings.settings1 = "Sample Data";
   7: settings.Update();

And the reading:

   1: //Read the settings
   2: MyJobSettings res = job.GetChild<MyJobSettings>("settings");
   3: string setting1 = res.setting1;

-- My name is Itay Shakury, and I’m a SharePoint consultant --

תגים:,

Comments

# re: Storing Configuration Settings For Timer Jobs

Saturday, March 24, 2012 1:01 AM by choroshin

Great Post!

# re: Storing Configuration Settings For Timer Jobs

Monday, November 19, 2012 7:23 PM by Navaneeth

How to use custom timer job scheduling in SharePoint 2010?

The scheduling information like min,hours should be read from custom list in SharePoint site and using feature receiver we need to pull the information from list and update the timer job definition.

How Can we read the site url and custom list url and send it to the feature receiver for updating the timer job definition?

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: