SharePoint 2010 Custom Timer Job

SharePoint 2010 定制计时任务
本文介绍如何在 SharePoint 2010 中创建自定义计时任务,包括计时任务的基本概念及其在 Windows 服务中的运行方式。通过示例代码演示了如何设置每五分钟执行一次的任务,并在指定列表中记录当前时间。

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

转载于:https://www.cnblogs.com/ilawrence/archive/2012/12/21/2828028.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值