Adding properties to a Custom SharePoint Timer Job

Posted: 16th March 2011 in SharePoint, Sharepoint 2010

I’ve never really had to do this before as most SharePoint jobs usually operate at Farm level. However, on this occasion I had to run the timer job of a specific Site Collection.

First stop of course was Google, where I found a lot of blogs spouting that the way to do it was to:

  • Add a public field
  • Add the Persisted() attribute to it as can be seen in the code snippet below
Persisted Public Field
  1. [Persisted()]
  2.         public string SiteCollectionUrl;

I might have been doing it wrong, but when I did this all I got was numerous errors in the SharePoint logs indicating that the field would not be upgradable.

The EmployeeDetailsSiteCollectionUrl field in the <CLASSNAME> class has been deleted.  The old value will be preserved in the UpgradedPersistedFields collection under the EmployeeDetailsSiteCollectionUrl key.  Consider writing upgrade code to copy this value to a new location.

So, after seeing this error I decided that this was not the correct way to do it.

Turns out there is a Properties collection property on the base class SPJobDefinition you can add to. To set the SiteCollection url I added the property in the feature receiver as seen in line 15 of the code snippet below. 

Setting a property
  1.         public override void FeatureActivated(SPFeatureReceiverProperties properties)
  2.         {
  3.             SPSite site = properties.Feature.Parent as SPSite;
  5.             // make sure the job isn't already registered
  6.             foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
  7.             {
  8.                 if (job.Name == Resources.EmployeeDetailsJobName)
  9.                     job.Delete();
  10.             }
  12.             // install the job
  13.             EmployeeDetailsJob employeeDetailJob = new EmployeeDetailsJob(Resources.EmployeeDetailsJobName, site.WebApplication);
  15.             employeeDetailJob.Properties.Add("EmployeeDetailsSiteCollectionUrl", site.Url);
  16.             SPWeeklySchedule schedule = new SPWeeklySchedule();
  17.             schedule.BeginSecond = 0;
  18.             schedule.EndSecond = 59;
  19.             employeeDetailJob.Schedule = schedule;
  20.             employeeDetailJob.Update();
  24.         }


Now, to access the property in the Job just use the code below.

Insert List Item Method
  1. this.Properties["EmployeeDetailsSiteCollectionUrl"].ToString()

  1. […] other workarounds but I decided I would use a custom property to pass information to the timer job, adding properties to a custom SharePoint Timer Job. The only down side of this is if I need to change the details being passed to the timer job I have […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s