zoukankan      html  css  js  c++  java
  • SharePoint Timer Job

    首先介绍一下什么是定时器作业,说的再多,也不如一张图说的清楚


    这两张图应该把我想说的已经表达清楚了,下一步介绍一下如何自定义Timer Job
    第一步:创建一个类(CustomTimerJob.cs)
    第二步:引用 using Microsoft.SharePoint.Administration;并继承SPJobDefinition
    第三步:构造 三个函数
            public CustomTimerJob()
                : base()
            {
            }
            public CustomTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
                : base(jobName, service, server, targetType)
            {
            }
            public CustomTimerJob(string jobName, SPWebApplication webApplication)
                : base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
            {
                this.Title = "Custom Timer Job";  //在上面的图处上可以看到这个名字
            }
    第四步:override Execute 处理自己的业务
         public override void Execute(Guid targetInstanceId)
            {
                SPWebApplication webApplication = this.Parent as SPWebApplication;
                SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
                // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database
                SPList taskList = contentDb.Sites[0].RootWeb.Lists["Product"];
                // create a new task, set the Title to the current day/time, and update the item
                SPListItem newTask = taskList.Items.Add();
                newTask["Title"] = DateTime.Now.ToString();
                newTask.Update();
            }
    上一篇 自定义 Features 里面,当激活功能时,到管理中心可以看到自己的定义的 Timer Job
    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            {
                SPSite site = properties.Feature.Parent as SPSite;
                if (site != null)
                {
                    foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                    {
                        if (job.Name == CUSTOM_TIMER_JOB)
                        {
                            job.Delete();
                        }
                    }
                }
                //开始Job
                CustomTimerJob customTimerJob = new CustomTimerJob(CUSTOM_TIMER_JOB, site.WebApplication);
                SPMinuteSchedule schedule = new SPMinuteSchedule();
                schedule.BeginSecond = 0;
                schedule.EndSecond = 59;
                schedule.Interval = 5;
                customTimerJob.Schedule = schedule;
                customTimerJob.Update();
            }
    之后部署,到网站网站集里停止再激活,之后去管理中心,就可以看到自己定义的Job了

    更多精彩请关注







  • 相关阅读:
    jsonp 原理和基本使用
    jsonplaceholder直接提供模拟测试数据
    为什么要学mock
    vue基础知识和案例
    winform datagridview中combobox列改变选项时触发其他列变化
    将DataGridView转换为DataTable
    C#中删除字符串最后一个字符串的几种方式
    C#中成员变量和局部变量的区别
    WinForm窗体中如何在一个窗体中取到另一个窗体的值
    ComboBox 中 DisplayMember 和 ValueMember有何区别
  • 原文地址:https://www.cnblogs.com/Fengger/p/2532047.html
Copyright © 2011-2022 走看看