Tuesday, July 20, 2010

Creating and Working with SharePoint Timer Job Definitions !

Hi All,

Many times we require creating a timer job definition because of requirement logic. Let’s say, we have some requirement something like whenever list gets added to the site, site administrator should get email saying that this list is being added by so and so person at this time.

Second example, you are supposed to write a job which starts the workflow every night for an approval process of some data calculations.

Now we do not have anything like ListAdding and ListAdded event that gives the answer of our question or any other event that solves some requirement like second example.

In these cases, we can create custom timer job and specify when to run and what to do when it runs.

So let us go ahead and create a custom timer job.

For this, I suggest creating a feature class which installs the job definition when activated and removes the job definition when feature is deactivated or other way is to go to timer job definitions under Operations in central administration and disable that specific job definition.

Before starting up the example, I also would like to explain you few classes for scheduling. Depending upon the schedule that you want to set, you need to take the classes. For example we have SPWeeklySchedule, SPDailySchedule, SPMonthlySchedule, SPYearlySchedule, SPHourlySchedule, SPMinuteSchedule.

You must have got the idea by the name of these classes when to use them. If you want to run your job every hour, you have SPHourlySchedule; if you want it to run every minute then you have SPMinuteSchedule. You can get the idea about the properties that needs to be set when using respective classes from searching MSDN.

Here I am taking an example of SPMinuteSchedule to explain you the example. Do keep in mind that different classes have different properties to be set.

This gives you an idea that when feature will be activated at that time job will be scheduled, if exists then removed and then scheduled and at the time of deactivating job will be removed. When you will activate the feature you will find the Job name in the Timer Job Definitions in the operations section of central administration under global configuration section and you can also see the timer job status but you won’t find your custom timer job there untill it executes atleast once. This will run every 5 minutes.

Here I also would like to share is what is the significance of BeginSecond and EndSecond and any such propoerty for Begin and End for any above class is that timer job starts at any random time between sub specified time durations. This is because if you have many timer jobs running at the same time, then it could put a heavy load on server.

Do not go deep into the logic, as this is just to give an idea about the creation of timer job and along with that simple logic which checks for the lists created by default at the time of site creation which will have the created time same as time created for site.

What I am doing here is first I am storing List Names in the Tasks List (Just to demonstrate you, you can change the logic suiting to your need). I get all items from the tasks list and compare stored Title (List Name) with the each list name of the site, if not there that means it is newly created, send mail to admin and then add the list name again to tasks list title column. So this will keep on working and every five mintes, it will keep checking if newly list has been added or what and sends mail.

Please note that logic is not perfact, just to demonstrate you and give you an idear about Timer job definition and at the same time idea about how to implement List added logic. You can share the best idea with us.

IMP : How to debug the timer job definitions.

Other very important thing is debugging the timer job definitions. Debugging timer job definition is something different because its not about attaching w3wp process because this doesn’t run under web. So we do have other process here to attach to get this thing debug. Name of the process is OWStimer.exe. When you attach this process, then just wait for your timer to get to the right defined time by you and then it will automatically come for debugging. Means if you have set it to 2 minutes, then attach the OWStimer.exe process and just wait for 2 minutes. Every 2 minutes you will get the breakpoint hitting the location.

Ok, one more thing. If you have observed the Timer job definition in central administration, then it gives you an option to disable the job definition but it doesn’t allow to execute it at force.

One way to go about this is to use the stsadm –o execadmsvcjobs but the problem with this is it will execute all the administrative timer jobs without waiting for any job to run in schedule because it doesn’t take any parameter.

So solution is again code. Here is that one liner code that does stuff for us.

No comments:

Post a Comment

About Me

A SharePoint Developer,I have worked for several consulting companies, and developed several SharePoint solutions. I have been awarded Application Development MCTS in MOSS from Microsoft on the May 2009.
And also I am a MS CRM Developer.
Please visit my MS CRM Blog
http://srikanthcrm.wordpress.com