Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Advancing from my original intent to run things via a Workflow as per this original question, I've started to work on a complex, configurable Timer Job that reads from each listitem to determine the appropriate flags for each action it takes.

However, most of the tutorials and demonstrations I find for this work on the Web Application level (as timer jobs are installed there). I need this to be run on a site collection level, and only on specific site collections (the selection of which will change over time).

How can I adapt a timer job to be scoped to specific site collections? In the feature that is responsible for adding the Timer Job, I've tried adding various properties of the current SPSite as a property of the custom SPJobDefinition, but they either don't stick, or don't work. I've tried storing the ServerRelativeUrl property (which the SPSiteCollection[] indexer says you can use to specify), I've tried using First(s => s.ServerRelativeUrl.Equals(storedvalue)) on the SPSiteCollection, and variations on the container to store these items. Nothing has worked.

  • How can I properly specify inside the Timer Job what site collections should be looked at? How can I properly reference and modify these values as site collections phase in and out of production?
  • If it is not possible to store this in the Timer Job itself, how best should I iterate the site collections and determine which ones need to actually run the process? The number of sites may grow very fast so it would ideally need to account for an increasing number of site collections to run by.
share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

You can store the site id and other values on the web application property bag. Here is an example on setting and updating the webpapplication properties

SPWebApplication webApp = SPWebApplication.Lookup(new Uri(ddlTargetWebApp.SelectedValue));

//I use the site and and the key to identifying them 
if (webApp.Properties.ContainsKey(siteId + key)) {
   webApp.Properties[siteId + key] = value;
}
else
{
   webApp.Properties.Add(siteId + key, value);
}
webApp.Update();

Hope this helps.

share|improve this answer
1  
This worked like a charm. I'm able to activate and deactivate the feature on different site collections, and the timer job flawlessly adapts to it. Thank you much! ♪ –  Grace Note Apr 14 '11 at 18:52
    
Glad to be helpfull! –  Renzo Apr 15 '11 at 14:28
add comment

Could bung your site collection references that you want to store into an SPPersistedObject.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.sppersistedobject.aspx

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.