zoukankan      html  css  js  c++  java
  • 一个SharePoint定时器 (SPJobDefinition)

    需要写一个自定义的sharepoint timer job, 目的是要定时到Site Collection Images这个List里检查图片的过期日期,如果即将过期的话,需要发送email到相关的人员。

    GOOGLE了一下,发现资料很少。不过幸运的是我发现了 Andrew Connell的博客,上面有一个很详细的示例代码。http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx

    代码的基本思路是:我们的自定义job类需要从SPJobDefinition继承过来,并且要改写几个构造体和一个Execute方法。其中不带参数的默认构造体是必须的。

    那么在Execute方法中怎么得到这个SPWebApplication对象呢?AC用了如下的代码:

    SPWebApplication webApplication = this.Parent as SPWebApplication;

    用Parent属性并且CAST成WebApplication对象, 这段我比较困扰,因为根据SDK里的说明,本来就有一个WebApplication属性可以用,为什么还要这么麻烦呢。我试了如下代码

    SPWebApplication webApplication = this.WebApplication;

    测试结果发现完全等效。

    好,这个类写完以后,我们还要写另外一个SPFeatureReceiver类。因为我们需要一个Feature来把我们的Timer部署到服务器上去,通过Feature Activated/deactivated来触发/关闭这个TimerJob。

    public overridevoid FeatureActivated(SPFeatureReceiverProperties properties)

            {

                SPSite site = properties.Feature.Parentas SPSite;

                SPWeb web = site.RootWeb;

               

                // make sure the job isn't already registered

                foreach (SPJobDefinition jobin site.WebApplication.JobDefinitions)

                {

                    if (job.Name == "ImageValidationCheckerJob")

                    {

                        job.Delete();

                    }

                }

                // install the job

                ImageValidationCheckerJob imageValidationCheckerJob =new ImageValidationCheckerJob("ImageValidationCheckerJob", site.WebApplication);

                SPDailySchedule schedule = new SPMonthlySchedule();

                schedule.BeginHour = 23;

                schedule.BeginMinute = 40;

                schedule.BeginSecond = 1;           

                schedule.EndHour = 23;

                schedule.EndMinute = 59;

                schedule.EndSecond = 1;

                imageValidationCheckerJob.Schedule = schedule;

                imageValidationCheckerJob.Update();

            }

    public overridevoid FeatureDeactivating(SPFeatureReceiverProperties properties)

            {

                SPSite site = properties.Feature.Parentas SPSite;

               

                // delete the job

                foreach (SPJobDefinition jobin site.WebApplication.JobDefinitions)

                {

                    if (job.Name == "ImageValidationCheckerJob")

                    {

                        job.Delete();

                    }

                }

            }

    这个SPDailySchedule对象让我有些困扰,我现在理解是你要给定开始时间和结束时间,然后系统会随机算出一个在两者中间的时刻来启动这个工作。比如上面例子,那么每天运行时间应该是介于23:40:1 到23:59:1 之间, 不知道是不是这样,不过我猜测应该是如此。

    代码写完了以后,还要写一个Feature,这个Feature比较简单,就一个XML文件

    Feature.xml

    <?xml version="1.0" encoding="utf-8" ?>

    <Feature xmlns="http://schemas.microsoft.com/sharepoint/"

             Id="1F481C17-4FDA-4919-A64A-EAE5C1301B4B"

             Title="Image Validation Checker"

             Description="If any images in the top level site colleciton images are expiring soon,email relative person."

             Scope="Site"

             Hidden="TRUE"

             Version="1.0.0.0"

             ReceiverAssembly="TimerJobControl, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f2aef6a9088f714f"

    ReceiverClass="TimerJobControl.ImageValidationCheckerJobInstaller">

    </Feature>

    剩下的就是把feature装好就好了,假如代码没有出错的话。

    至于怎么Debug这个程序,走以下步骤:(有些步骤有时候可能不用,但是全用可以保证没有问题,这个是我花了1天的时间得出的血的教训)

    (为了调试方面,把Schedule设置为SPMinuteSchedule,并且设置为每2分钟运行一次)

    1. 把Assembly DLL放到 GAC
    2. 命令行:iisreset
    3. 先Deactivate feature, 然后activate feature.
    4. 命令行:net stop SPTimerV3
    5. 命令行:net start SPTimerV3
    6. Visual Studio: Attach to process: OWSTIMER.EXE
    7. 完。

    还有一点要注意的是,如果你想在TimerJob类里面要从web.config文件得到一些值得话,我觉得是不可能的,至少我还没有发现该怎么做,所以,假如代码需要从外部取得一些信息的话,

    解决办法是 :在 c:/program files/common files/microsoft shared/…/12/bin目录里新建一个文件,文件名叫做:OwsTimer.exe.config,

    <configuration>

      <appSettings>

       <add key="YourKey" value="YourValue" />

      </appSettings>

    </configuration>

    然后用ConfigurationManager.AppSettings.Get("YourKey"); 来取得这个值。

    记住每次修改代码以后进行调试前,一定要走上面说的步骤,特别是步骤4和5很容易被忘记,不然可能会遇到很莫名其妙的问题。

    最后,happy SharePointING。

  • 相关阅读:
    PAT Basic 1077 互评成绩计算 (20 分)
    PAT Basic 1055 集体照 (25 分)
    PAT Basic 1059 C语言竞赛 (20 分)
    PAT Basic 1072 开学寄语 (20 分)
    PAT Basic 1049 数列的片段和 (20 分)
    蓝桥杯BASIC-13 数列排序
    蓝桥杯入门——3.序列求和
    蓝桥杯入门——2.圆的面积
    蓝桥杯入门——1.Fibonacci数列
    树的总结(遍历,BST,AVL原型,堆,练习题)
  • 原文地址:https://www.cnblogs.com/zhijianliutang/p/3374387.html
Copyright © 2011-2022 走看看