sharepoint 2010 如何创建一个timer job

在sharepoint的开发和应用中,经常会使用到,需要定时执行或者更新数据,我们可以用sharepoint自带的timer job来实现。

1。创建一个sharepoint 项目,名称为TimerJobTest

2。创建一个class文件,名称为TimerJobClass;继承SPJobDefinition,如下图

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint.Administration;

namespace TimerJobTest

{

    public class TimerJobClass : SPJobDefinition

    {

        public TimerJobClass(): base(){}

        public TimerJobClass(string jobName, SPService service, SPServer server,

            SPJobLockType targetType)

            : base(jobName, service, server, targetType)

        {

 

        }

        public TimerJobClass(string jobName, SPWebApplication webApplication)

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

        {

            this.Title = jobName;

        }

 

        public override void Execute(Guid contentDbId)

        {

           //这里就是我们要执行的函数方法

        }

    }

}

3。添加一个feature,名称为TimerJob,并且选择范围为site,如下图:

4。添加一个事件接收器,如下图:

5。需要override其中的两个函数,

override void FeatureActivated(SPFeatureReceiverProperties properties),部署timer job函数

override void FeatureDeactivating(SPFeatureReceiverProperties properties) 删除timer job函数

方法如下:

const string JOB_NAME = "TimerJobTest";

        // 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 == JOB_NAME)

                {

                    job.Delete();

                }

            }

            // install the job

            TimerJobClass Doc = new TimerJobClass(JOB_NAME, site.WebApplication);

            SPMinuteSchedule schedule = new SPMinuteSchedule();

            schedule.BeginSecond = 0;

            schedule.EndSecond = 59;

            schedule.Interval = 1;

            Doc.Schedule = schedule;

            Doc.Update();

        }

        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 == JOB_NAME)

                {

                    job.Delete();

                }

            }

        }

6。部署之后,到管理中心,作业定义中,查看是否已经部署成功,如下图,我们看到,timerjobTest已经成功部署,如下图

7。需要重新启动服务,如下图

这时候我们的timer job 就创建完成了。

附件下载地址:http://download.csdn.net/download/cxx2325938/4889649

广州京微信息科技有限公司,.微软sharepoint解决方案提供商。

 

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值