2-13 SharePont Timer Job 定时器

1.新建timerjob类

2.添加feature 功能

 

3.修改功能名称

4.设置功能范围为网站集

 

5.feature 功能添加事件接收器

 

6.计时器服务 所在位置  SharePoint Timer Service

 

7.管理中心,管理计时器任务   监控 复查作业定义

 

 

 

 

8. 计时器进程  OWSTIMER.EXE 调试计时器时 进行附加

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace CustomTimerJob
{
    class DemoCustomTimerJob:SPJobDefinition
    {
        public DemoCustomTimerJob() : base() { }

        public DemoCustomTimerJob(string TimerName, SPWebApplication webapp)
            : base(TimerName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "CustomTimerJob";
        }
        public override void Execute(Guid targetInstanceId)
        {
            using (SPSite site = new SPSite("http://sp2016:8000/sites/topsite/"))
            {
                using (SPWeb web = site.OpenWeb(""))
                {
                    SPList list = web.GetList("/sites/topsite/Lists/ListCode");
                    SPListItemCollection itmcol = list.Items;
                    for (int i = 0; i < itmcol.Count; i++)
                    {
                        SPListItem item = list.GetItemById(itmcol[i].ID);
                        item["Title"] = "updated by timerJOB";
                        item.Update();
                    }
                }
            }
        }

    }
}
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace CustomTimerJob.Features.CustomTimerJob
{
    /// <summary>
    /// 此类用于处理在激活、停用、安装、卸载和升级功能的过程中引发的事件。
    /// </summary>
    /// <remarks>
    /// 附加到此类的 GUID 可能会在打包期间使用,不应进行修改。
    /// </remarks>

    [Guid("8c668b21-c533-410c-9818-4e5887ae6ef1")]
    public class CustomTimerJobEventReceiver : SPFeatureReceiver
    {
        // 取消对以下方法的注释,以便处理激活某个功能后引发的事件。

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            const string TimerJobName = "CustomTimerJob";
            SPSite site = properties.Feature.Parent as SPSite;
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
            {
                if (job.Title == TimerJobName)
                { job.Delete(); }
            }
            DemoCustomTimerJob UpdateTitle = new DemoCustomTimerJob(TimerJobName, site.WebApplication);
            SPMinuteSchedule minuteSchedule = new SPMinuteSchedule();
            minuteSchedule.BeginSecond = 0;
            minuteSchedule.EndSecond = 59;
            minuteSchedule.Interval = 1;
            UpdateTitle.Schedule = minuteSchedule;
            UpdateTitle.Update();

        }


        // 取消对以下方法的注释,以便处理在停用某个功能前引发的事件。

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // 取消对以下方法的注释,以便处理在安装某个功能后引发的事件。

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // 取消对以下方法的注释,以便处理在卸载某个功能前引发的事件。

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // 取消对以下方法的注释,以便处理在升级某个功能时引发的事件。

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}

 

posted @ 2020-05-25 17:52  七秒钟得记忆  阅读(99)  评论(0)    收藏  举报