TimerJob

  1. 列表内容

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

这里写图片描述
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
这里写图片描述
5. 部署程式,从Central Administration中激活feature
这里写图片描述
6. 需要将dll 注册到GAC,遇到过Job Instance 创建成功,但是不会执行的情形(上次运行时间一直是N/A)。可能因为Timer Job 是通过SharePoint Timer Service 触发的,SharePoint Timer Service 会到GAC中找是否有实现Execute 方法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值