1)编写计时器程序
         建一个类库,命名为EventCheckTimer,代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace EventCheckTimer
{
    class EventCheckTimer : SPJobDefinition
    {
        public EventCheckTimer() : base() { }
        public EventCheckTimer(string _timername, SPWebApplication _wp): base(_timername, _wp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "提醒器EventCheckTimer";
        }

        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webApplication = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
            foreach (SPListItem li in contentDb.Sites[0].AllWebs[0].Lists["AZTAList"].Items)
            {
                 li["TestColumn"] = " SPJob Update: " + DateTime.Now.ToShortDateString();
                li.Update();
            }      
        }
    }
}

2)编写部署这个计时器所需要的安装器
      新建一个.cs文件,命名为EventCheckTimerInstaller。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace EventCheckTimer
{
    public class EventCheckTimerInstaller : SPFeatureReceiver
    {
        const string MY_TASK = "EventCheckTimer";

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == MY_TASK)
                    job.Delete();
            }
            EventCheckTimer timer = new EventCheckTimer(MY_TASK, site.WebApplication);
            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 1;
            timer.Schedule = schedule;
            timer.Update();
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == MY_TASK)
                    job.Delete();
            }
        }
    }
}
制作完后,添加强命名密钥后,整个工程如下:

   
3)制作feature.xml并部署
在C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES下新建文件夹EventCheckTimerInstaller,添加一个feature.xml文件,编写如下:

<?xml version="1.0" encoding="utf-8" ?>
<Feature  Id="DC421BF5-6EAE-4d2b-A1E3-CF3B987DEC10"
          Title="EventCheckTimer;"
          Description="EventCheckTimer;"
          Version="12.0.0.0"
          Scope="Site"
          xmlns="http://schemas.microsoft.com/sharepoint/"
          ReceiverAssembly="EventCheckTimer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=624862d06d2a2256"
          ReceiverClass="EventCheckTimer.EventCheckTimerInstaller">
</Feature>

利用命令部署feature。
stsadm -o installfeature -filename EventCheckTimer\feature.xml
stsadm
 -o activatefeature -filename EventCheckTimer\feature.xml -url  http://<siteurl>

4)测试效果。
 在管理中心>操作>定时器作业定义 中可见:

任务中可见:


 

 

注意:如果修改了JOB程序再部署了,我们必须要重启 OWSTIMER服务,才可以生效.

posted on 2010-12-21 10:18  Roy Cao  阅读(199)  评论(0)    收藏  举报