SharePoint 2013 Timejob

</pre>很多应用程序都需要在指定的时间<span style="font-family:Calibri;">(</span><span style="background:yellow">每年</span><span style="background:yellow"><span style="font-family:Calibri;">,</span></span><span style="background:yellow">每月</span><span style="background:yellow"><span style="font-family:Calibri;">,</span></span><span style="background:yellow">每周</span><span style="background:yellow"><span style="font-family:Calibri;">,</span></span><span style="background:yellow">每天</span><span style="background:yellow"><span style="font-family:Calibri;">,</span></span><span style="background:yellow">每小时</span><span style="font-family:Calibri;">)</span>自动在后台重复执行某个流程<span style="font-family:Calibri;">,</span>一般为批量处理一些记录<span style="font-family:Calibri;">,</span>执行比较长的操作等<span style="font-family:Calibri;">.</span>在<span style="font-family:Calibri;">SharePoint2013/2010</span>中,<span style="font-family:Calibri;">Timer Jobs</span>负责<span style="font-family:Calibri;">host</span>这些重复执行的流程,以及他们的排程<span style="font-family:Calibri;">(Schedule),</span>本文将以<span style="font-family:Calibri;">SharePoint2013</span>为例介绍如何使用<span style="font-family:Calibri;">TimerJobs</span>,但是<span style="font-family:Calibri;">90%</span>的步骤和概念是和<span style="font-family:Calibri;">SharePoint2010</span>中一致的。<p></p><p> 修改改过之后要重新启动SharePoint Time server</p><p>附加进程:OWSTmer.exe进行调试</p><p><span style="font-size:14px;">可以在管理中心查看<span style="font-family:Calibri;font-size:14px;">Timer Jobs</span>(系统自带有很多<span style="font-family:Calibri;font-size:14px;">Timer Jobs</span>):</span></p><p><span style="font-size:14px;"><img alt="" src="https://img-my.csdn.net/uploads/201302/21/1361440776_3256.png" /></span></p><p><span style="font-size:14px;"><img alt="" src="https://img-my.csdn.net/uploads/201302/21/1361440800_9218.png" height="398" width="860" /></span></p><p><span style="font-size:14px;"></span> </p><span style="font-size:14px;"></span><p><span style="font-family:Calibri;font-size:14px;">SharePoint Timer Service </span>负责执行了调度所有的<span style="font-family:Calibri;"> TimerJob</span>,因此你需要确保运行该服务的账号有访问<span style="font-family:Calibri;">TimerJob</span>里面需要用到的资源的权限。</p><p><img alt="" src="https://img-my.csdn.net/uploads/201302/21/1361440840_4704.png" /></p><p> </p><p><span style="font-size:14px;">下面介绍如何在<span style="font-family:Calibri;">SharePoint 2013:</span></span></p><p>1. 创建一个Empty的SharePoint solution(Farm) ,并建立如下代码结构</p><p><img alt="" src="https://img-my.csdn.net/uploads/201302/21/1361440946_9931.png" /></p><p>MyTimerJob.cs 负责定义 SPJobDefinition:</p><div class="dp-highlighter bg_csharp"><pre code_snippet_id="248950" snippet_file_name="blog_20140321_2_1482402" name="code" class="csharp">    // 首先是要自定义一个类,继承自 SPJobDefinition  
    public class MyTimerJob : SPJobDefinition
    {
        // 该构造只用于序列化和反列化,因为timer job的Definition 初始化后成为instance需要保存在数据库中  
        public MyTimerJob()
            : base()
        {

        }

        // 创建针对SPService application的timer job 是调用的构造  
        public MyTimerJob(string jobName, SPService service, SPServer server, SPJobLockType lockType)
            : base(jobName, service, server, lockType)
        {
            this.Title = "Products Timer Job";
        }

        // 创建针对web application的timer job 是调用的构造  
        public MyTimerJob(string jobName, SPWebApplication webapp)
            : base(jobName, webapp, null, SPJobLockType.ContentDatabase)
        {
            this.Title = "Products Timer Job";
        }

        // 每次执行到Timer Job时要执行的方法。跟据Timer Job 的Schedule  
        public override void Execute(Guid targetInstanceId)
        {
            SPWebApplication webapp = this.Parent as SPWebApplication;
            SPContentDatabase contentDb = webapp.ContentDatabases[targetInstanceId];

            // 取参数,通过Properties可以从外部传递参数,但要求参数可以被序列化和反列化  
            string siteUrl = "http://demo1";
            using (SPSite site = new SPSite(siteUrl))
            {
                // 这里可写自己的逻辑 
                SPWeb spWeb = site.OpenWeb();
                spWeb.AllowUnsafeUpdates = true;
                
                SPList spList = spWeb.Lists["Test"];

                SPListItem li = spList.AddItem();

                li["Title"] = "测试";

                li.Update();

            }

            // 这里可写自己的逻辑   
        }
    }
MyTimerJob Feature.EventReceiver.cs 负责实例化SPJobDefinition,设置排程(schedule),传递参数:

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

    [Guid("ea4c4f86-be84-4a69-b2ec-1762eaf28560")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // 取消对以下方法的注释,以便处理激活某个功能后引发的事件。
        const string JobName = "My Timer Job"; 
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // 注意此处as 后面是SPSite还是SPWeb, SPFarm ...要根据Feature 的Scope  
            SPSite site = new SPSite("http://demo1");

            DeleteJob(site); // Delete Job if already Exists  

            CreateJob(site); // Create new Job  
        }


        private static void DeleteJob(SPSite site)
        {
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)
                if (job.Name == JobName)
                    job.Delete();
        }

        private static void CreateJob(SPSite site)
        {

            // 初始化 SPJobDefinition  
            MyTimerJob job = new MyTimerJob(JobName, site.WebApplication);

            // 传递参数  
            job.Properties.Add("SiteUrl", site.Url);

            // 设置 schedule, 可选 SPDailySchedule, SPWeeklySchedule, SPYearlySchedule,SPMonthlyByDaySchedule,SPHourlySchedule,SPMinuteSchedule            

            SPMinuteSchedule schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 5;
            schedule.Interval = 5;

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

        }  

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

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            // 删除 Timer Job的实例  
            DeleteJob(properties.Feature.Parent as SPSite); // Delete the Job  
        }


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

        //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)
        //{
        //}
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值