One task that I’ve been threatening to do for ages is to build a SharePoint Timer Job. Often a solution needs a bit of code to run periodically, but so far it wasn’t something that I’d tried. Well, I had a go today following Andrew Connell’s MSDN article, and it was very good. Here are my notes though…
First up, I used WSPBuilder’s project definitions. It’s much easier to build a WSP this way, and the ‘Blank Feature with Receiver’ template is very useful, as I used an SPFeatureReceiver to activate the job.
All my job did was, well, prove it was running by writing to a local log file. I didn’t do all of the stuff Andrew Connell wrote about regarding configuration – while creating an administration page would be an interesting exercise, I don’t think that we’ll do that often in practice. More likely are external configuration files, and that’s pretty simple.
Anyway, other than that the only puzzle was that my Feature is scoped at a farm level, and the example feature receiver wasn’t scoped at this level. So, if I’m working at the farm level, what is my feature’s parent?
Well, some poking around showed this to be the SPWebService. Thus, to delete the job on feature deactivation, the code was:
SPWebService svc = properties.Feature.Parent as SPWebService
foreach (SPJobDefinition job in svc.JobDefinitions)
if(job.Name == "MyServiceName")
Other than that, it was all pretty simple.