I've added a 2nd site collection with its own content db. My custom timer job now runs twice, I want it to run only once as it sends an email out.

How can I achieve this ?

Here is my code :

I've commented a few lines - contentDb.Sites[0].... (and I cant remember why) Could this be the answer ?

Also in previous post there was mention of 'If (mySiteCollection.ContentDatabase.Id.Equals(contentDbId))' but couldnt successfully target mySiteCollection ?

    namespace DT_CustomTimerDriveAlert
    class DriveAlert : SPJobDefinition
         public DriveAlert() 

            : base()


        public DriveAlert(string jobName, SPService service, SPServer server, SPJobLockType targetType)

            : base(jobName, service, server, targetType)


        public DriveAlert(string jobName, SPWebApplication webApplication)

            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)

                    this.Title = jobName;


        public override void Execute(Guid contentDbId)

                  // get a reference to the current site collection's content database

                  SPWebApplication webApplication = this.Parent as SPWebApplication;

                  //SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

                  // get a reference to the "ListTimerJob" list in the RootWeb of the first site collection in the content database

                  // -- SPList Listjob = contentDb.Sites[0].RootWeb.Lists["DT_TimerJob"];

                  SPSite site = webApplication.Sites["http://sp20104/sites/apps"];

                  SPWeb web = site.OpenWeb("mysite");
                  SPList Listjob = web.Lists["mylist"];

1 Answer 1


You want to set the SPJobLockType, which you've done for one of your constructors - that looks right to me. That's what I've always used. You might find that an SPJobLockType.Job is more appropriate, though that's not clear from your code.

Are you sure you don't have two timer jobs defined? Or one job running for 2 content databases that grabs the same content from one of the content DBs?

Though to be honest, from what you're describing, I wonder if a SPWorkItemJobDefinition wouldn't be a better fit. There's a good post about it here, but in short, it allows you to register things that you want to process - rather than running on each content database and then checking withing that content db.

  • Hi I I've got it working by using SPJobLockType.Job. About the two constructors, I will try to delete : public DriveAlert(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType) Thanks Jul 31, 2012 at 13:41
  • does SPJobLockType.Job actually restrict it to only one content database? if so, how does it know which database? I have set it from ContentDatabase to Job and it still runs on all content databases... Is there any way around this besides code to check which content database it is currently running on?
    – Nacht
    Oct 18, 2012 at 6:08

Your Answer

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

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