Storing Configuration Settings For Timer Jobs

23 במרץ 2012

תגיות: ,
2 תגובות

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:

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");


   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 —

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *

2 תגובות

  1. choroshin24 במרץ 2012 ב 1:01

    Great Post!

  2. Navaneeth19 בנובמבר 2012 ב 19:23

    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?