Introduction

Based on my popular articel about Creating Custom Timer Job in Sharepoint 2010,I Have decide to upload the simple code  on how to create Custom Timer Job in Sharepoint 2010 ,But first let us know more about Timer Job

  • A timer job runs in a specific Windows service for SharePoint Server.
  • Timer jobs also perform infrastructure tasks for the Timer service, such as clearing the timer job history and recycling the Timer service; and tasks for Web applications, such as sending e-mail alerts.
  • A timer job contains a definition of the service to run and specifies how frequently the service is started.
  • The SharePoint 2010 Timer service (SPTimerv4) runs timer jobs.
  • Many features in SharePoint Server rely on timer jobs to run services according to a schedule.

Building the Sample

To run the sample you just need to open it in Visual Studio 2010 then deploy it to your test machine or you can package it to deploy it in Production environment.The solution itself will create list for you and try to fill it each five minutes.

Description

Based on my knoladge Customer Timer job in sharepoint 2010 is on of the most confused approch in SharePoint Many developer stuck at some point and they do not what to do to make it run correctly.So this sample will show you how to make it happent and run Custom Timer Job to achive you tasks in simple way.

C#
//Create class derived from SPJonDefinition Class 
 class ListTimerJob : SPJobDefinition 
    { 
         public ListTimerJob() 
 
            : base() 
        { 
 
        } 
 
        public ListTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType) 
 
            : base(jobName, service, server, targetType) 
        { 
 
        } 
 
        public ListTimerJob(string jobName, SPWebApplication webApplication) 
 
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase) 
        { 
 
            this.Title = "List Timer Job"; 
 
        } 
 
        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["ListTimerJob"]; 
 
            // create a new list Item, set the Title to the current day/time, and update the item 
 
            SPListItem newList = Listjob.Items.Add(); 
 
            newList["Title"] = DateTime.Now.ToString(); 
 
            newList.Update(); 
 
        } 
} 
//Add Event receiver at Feature Level  
[Guid("9a724fdb-e423-4232-9626-0cffc53fb74b")] 
public class Feature1EventReceiver : SPFeatureReceiver 
    { 
        const string List_JOB_NAME = "ListLogger"; 
        // Uncomment the method below to handle the event raised after a feature has been activated. 
 
        public override void FeatureActivated(SPFeatureReceiverProperties properties) 
        { 
            SPSite site = properties.Feature.Parent as SPSite; 
 
            // make sure the job isn't already registered 
 
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) 
            { 
 
                if (job.Name == List_JOB_NAME) 
 
                    job.Delete(); 
 
            } 
 
            // install the job 
 
            ListTimerJob listLoggerJob = new ListTimerJob(List_JOB_NAME, site.WebApplication); 
 
            SPMinuteSchedule schedule = new SPMinuteSchedule(); 
 
            schedule.BeginSecond = 0; 
 
            schedule.EndSecond = 59; 
 
            schedule.Interval = 5; 
 
            listLoggerJob.Schedule = schedule; 
 
            listLoggerJob.Update(); 
 
        } 
 
        // Uncomment the method below to handle the event raised before a feature is deactivated. 
 
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
        { 
            SPSite site = properties.Feature.Parent as SPSite; 
 
            // delete the job 
 
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) 
            { 
 
                if (job.Name == List_JOB_NAME) 
 
                    job.Delete(); 
 
            } 
 
}  
//That's all

Source Code Files

  • SharePoint Project (CustomTimerJob) - contain List I
  • Source  ------http://code.msdn.microsoft.com/office/SharePoint-2010-Custom-416cd3a1
posted on 2012-12-21 15:18  ilawrence  阅读(353)  评论(0编辑  收藏  举报