欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TimerJob

程序员文章站 2024-03-15 14:10:00
...
  1. 列表内容

这两天研究sharepoint 2013 创建timer job 的过程,将创建方法记录下来,供以后使用:

TimerJob
1.新建空的sharepoint 工程
2.增加Timer Job class

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

namespace Portal.TimerJob
{
    public class TimerJobTest:SPJobDefinition
    {

        public TimerJobTest(): base(){ }

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

            SPJobLockType targetType)

            : base(jobName, service, server, targetType)
        {
            this.Title = jobName;
            this.Name = jobName;

        }

        public TimerJobTest(string jobName, SPWebApplication webApplication)

            : base(jobName, webApplication, null, SPJobLockType.None)
        {
            this.Title = jobName;
            this.Name = jobName;
        }

        public override void Execute(Guid contentDbId)
        {
            try
            {
                LogHelper.GetLogHelper().createLog("GYZQ Portal TimerJobTest Execute");
                string siteCount = this.Properties["SiteCount"].ToString();
                LogHelper.GetLogHelper().createLog("SiteCount:" + siteCount);
                SPWebApplication webapp = this.Parent as SPWebApplication;
                SPContentDatabase contentDb = webapp.ContentDatabases[contentDbId];
                LogHelper.GetLogHelper().createLog("End:");
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().createExMsgLog(String.Format("Execute {0},{1}", ex.Message, ex.StackTrace));
                throw;
            }

        }

    }
}

3.修改feature name,修改scope 为WebApplication,并增加EventReceiver(用来创建或者卸载job instance)

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

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

    [Guid("6de2e176-2df2-463e-b315-be36e2a69646")]
    public class TimeJobTestEventReceiver : SPFeatureReceiver
    {
        // 取消对以下方法的注释,以便处理**某个功能后引发的事件。

        const string JobName = "GYZQ Portal TimerJob";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                LogHelper.GetLogHelper().createLog("FeatureActivated GYZQ Portal TimerJob");
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    SPWebApplication site = properties.Feature.Parent as SPWebApplication;
                    DeleteJob(site);
                    CreateJob(site);
                });
            }
            catch (Exception ex)
            {

                LogHelper.GetLogHelper().createExMsgLog(String.Format("FeatureActivated {0},{1}", ex.Message, ex.StackTrace));
            }

        }

        private static void DeleteJob(SPWebApplication site)
        {
            foreach (SPJobDefinition job in site.JobDefinitions)
            {
                LogHelper.GetLogHelper().createLog(job.Name);
                if (job.Name == JobName)job.Delete();
            }

        }

        private static void CreateJob(SPWebApplication site)
        {
            TimerJobTest job = new TimerJobTest(JobName, site);
            foreach(SPSite siteColl in site.Sites)
            {
                LogHelper.GetLogHelper().createLog(siteColl.Url);
            }
            job.Properties.Add("SiteCount", site.Sites.Count);
            SPHourlySchedule schedule = new SPHourlySchedule();
            //SPMinuteSchedule schedule = new SPMinuteSchedule();

            schedule.BeginMinute = 30;
            schedule.EndMinute = 40;

            job.Schedule = schedule;
            job.Update();
        }


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

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                LogHelper.GetLogHelper().createLog("FeatureDeactivating GYZQ Portal TimerJob");
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    DeleteJob(properties.Feature.Parent as SPWebApplication);
                });
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().createExMsgLog(String.Format("FeatureDeactivating {0},{1}", ex.Message, ex.StackTrace));
            }

        }


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

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            try
            {
                LogHelper.GetLogHelper().createLog("FeatureInstalled GYZQ Portal TimerJob");
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    SPWebApplication site = properties.Feature.Parent as SPWebApplication;
                    foreach (SPJobDefinition job in site.JobDefinitions)
                    {
                        if (job.Name == JobName) throw new NotImplementedException("Timer Job already installed");
                        CreateJob(site);
                    }
                });
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().createExMsgLog(String.Format("FeatureInstalled {0},{1}", ex.Message, ex.StackTrace));
            }

        }


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

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            try
            {
                LogHelper.GetLogHelper().createLog("FeatureUninstalling GYZQ Portal TimerJob");
                SPSecurity.RunWithElevatedPrivileges(delegate ()
                {
                    SPWebApplication site = properties.Feature.Parent as SPWebApplication;
                    DeleteJob(site);
                });
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().createExMsgLog(String.Format("FeatureUninstalling {0},{1}", ex.Message, ex.StackTrace));
            }


        }

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

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

4. 修改feature 属性默认**为false
TimerJob
5. 部署程式,从Central Administration中**feature
TimerJob
6. 需要将dll 注册到GAC,遇到过Job Instance 创建成功,但是不会执行的情形(上次运行时间一直是N/A)。可能因为Timer Job 是通过SharePoint Timer Service 触发的,SharePoint Timer Service 会到GAC中找是否有实现Execute 方法

推荐阅读