1

What I am trying to accomplish here is different from the usual "Create and deploy a new custom timer job". So please co operate.

Scenario : I have around 200 lists in my site. Each list gets its data from a third part web service (cutom code / not BCS). The data retrieval happens via Timer Jobs. The Timer Jobs call the web service, gets the fresh data and updates the list items. This is currently working fine with multiple lists and 1 timer job to refresh the data.

Problem : Now the issue I am facing is, each of the lists have its own Refresh Frequency. Say List 1 to 30 needs to be refreshed every 5 mins. List 31 to 60 every 10 mins, etc. I am currently handling this by having a RefreshLastRun timestamp.

This works fine in most of the scenarios - unless the Web service returns large data, in which case it takes more than 5 to 10 mins for the Update to complete on 4 to 5 lists. At the same time, the other lists - they were supposed to be updated 5 mins ago, but are still waiting to get updated because the timer job is busy updating another list, and the refresh is in Que. This is a completely unaccepatable solution to my client because the list with lower frequency duration (5 mins) is of a higher priority than the lists getting refreshed every 10 mins.

My Question is : How do I create a new Timer Job for each list? Whenever I add a new list (through code), I want to create a Timer Job at the same time, which will take care of refreshing the data in this and this list only. This way, I will have a independent Timer Job for each list and dont have to depend on 1 timer job to take all the load. With this approach I will have 200 timer jobs running. Which is OK with my client.

I hope I am able to convey the message. My Timer Jobs are working fine. I dont need suggestions on how to get the timer job working etc. My question is, how do I create a new Job Definition dynamically through code (no deployment).

3
  • Maybe you need to change your architecture? Have you thought about creating web service for syncronization and calling it from workflow with different parameters?
    – Roman
    Commented May 7, 2014 at 6:03
  • Architecture change at this point of time is really not possible. And how would workflows help me here? This is more of a cron job.
    – Ubaid
    Commented May 7, 2014 at 12:36
  • I mean, if you have sync web service. You aren't limited using timer job. For example you can use workflows or even windows scheduler for call it.
    – Roman
    Commented May 7, 2014 at 13:03

1 Answer 1

1

Assuming that your custom timer jobs just inherit from SPJobDefinition you can instantiate a new instance of them, alter the Schedule property to suit your timings and update. It might pay to give the new instances unique names....

var timerJob = new CustomTimerJob(webApplication, siteUrl)
{
    Schedule = new SPDailySchedule 
    { 
        BeginMinute = 1;
        EndMinute = 2;
    }
    Title = "List XXX Timer Job";
};
timerJob.Update();

In terms of adding these when you add a new list instance, that's where you run into trouble. There are ListAdded and ListAdding events that you can hook into for list added.

You may have issues with the security context when these run as you'll need to be a fairly privileged account to add timer jobs.

3
  • There are ListAdded and ListAdding events but the problem OP will face is with the security context (lack of permissions to create time jobs). Commented Apr 25, 2014 at 8:56
  • Thanks! This worked. Can somebody Upvote this answer? I dont have enough rep to do that!
    – Ubaid
    Commented May 6, 2014 at 7:40
  • Thanks Naim! I was foolishly looking at the 2007 List Events :| Have edited to reflect this. Ubaid, glad that worked out!
    – GavinB
    Commented May 6, 2014 at 23:04

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.