Monday, June 4, 2012

How to create a timer job in SharePoint programmatically.

To add a Timer Job in SharePoint site through code we need to go through following steps. It will add a simple timer job to site which will update Task list in every five minutes with current Date and Time.

Step 1: Add a new class to Visual Studio which is inheriting from SPJobDefinition for getting this include this namespace :

 using Microsoft.SharePoint.Administration;

public class TimerJobAction : SPJobDefinition
    {
        public TimerJobAction()
            : base()
        {
        }

        public TimerJobAction(string jobName, SPService service, SPServer server, SPJobLockType targetType)
            : base(jobName, service, server, targetType)
        {
        }

        public TimerJobAction(string jobName, SPWebApplication webApplication)
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Task Logger";
        }

        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 "Tasks" list in the RootWeb of the first site collection in the content database
            SPList taskList = contentDb.Sites[0].RootWeb.Lists["Tasks"];
            // create a new task, set the Title to the current day/time, and update the item
            SPListItem newTask = taskList.Items.Add();
            newTask["Title"] = DateTime.Now.ToString();
            newTask.Update();
        }
    }

Step 2: Now add a new feature of a site scoped and put this code in the Feature Activation to associate timer job to particular site :

 public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPSite spSite = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition spJob in spSite.WebApplication.JobDefinitions)
            {
                if (spJob.Name == TASK_LOGGER_JOB_NAME)
                {
                    spJob.Delete();
                }
            }

            TimerJobAction timerJobAction = new TimerJobAction(TASK_LOGGER_JOB_NAME, spSite.WebApplication);

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 5;
            timerJobAction.Schedule = schedule;

            timerJobAction.Update();
        }

Step 3: Now to dissociate timer job from particular site we need to put the following code in Feature Deactivation :

 public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite spSite = properties.Feature.Parent as SPSite;
           
            foreach (SPJobDefinition spJob in spSite.WebApplication.JobDefinitions)
            {
                if (spJob.Name == TASK_LOGGER_JOB_NAME)
                {
                    spJob.Delete();
                }
            }

        }

Now deploy this feature to your site and activate it. It will update the Tasks in every 5 minute and you should check it in Central Admin that Timer Job is running and deployed properly. 


No comments:

Post a Comment