3

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.

2 Answers 2

5

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.

5
  • 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
    Commented Apr 14, 2011 at 18:52
  • Glad to be helpfull!
    – Renzo
    Commented Apr 15, 2011 at 14:28
  • whats is this ddlTargetWebApp ???
    – samolpp2
    Commented Jun 30, 2017 at 15:16
  • @SaMolPP is the Target Url of the Web Application
    – Renzo
    Commented Jul 4, 2017 at 12:41
  • I didnt get . Is it a SharePoint keyword, i can use inside a timer job.cs file?
    – samolpp2
    Commented Jul 4, 2017 at 13:58
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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