SharePoint 開發TimerJob 介紹


       項目需要寫TimerJob,以前也大概知道原理,不過,開發過程中,還是遇到一些問題,網上看了好多博客,也有寫的灰常好的,不過,自己還是想再寫一下,也算是給自己一個總結,也算給大家多一個參考吧。

       TimerJob項目結構,主要有兩個Class,一個是用來定義TimerJob功能的,一個是用來部署開發好的TimerJob的,分別繼承兩個不同的類。如下圖,先建一個如下結構的項目:

clip_image001

 

文件描述:

TimerJob定義類:ModifyTitle.cs(繼承自SPJobDefinition)

TimerJob安裝類:ModifyTitleInstall.cs(繼承自SPFeatureReceiver)

激活TimerJob的Feature.xml

添加強命名,因為將來生成的dll是要放到GAC里面去的

 

添加引用:

引用Microsoft.SharePoint.dll文件,兩個Class都需要添加下面命名空間

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

 

ModifyTitleInstall

public class ModifyTitleInstall : SPFeatureReceiver

{

const string TimerJobName = "ModifyTitleTimerJob";//TimerJob的標題

//激活TimerJob的方法

public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPSite site = properties.Feature.Parent as SPSite;

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)

{

//如果有相同的TimerJob,先刪除

if (job.Title == TimerJobName)

{

job.Delete();

}

}

ModifyTitle modifyTitle = new ModifyTitle(TimerJobName, site.WebApplication);

SPMinuteSchedule minuteSchedule = new SPMinuteSchedule();//計時器對象

minuteSchedule.BeginSecond = 0;

minuteSchedule.EndSecond = 59;

minuteSchedule.Interval = 1;

modifyTitle.Schedule = minuteSchedule;

modifyTitle.Update();

//throw new NotImplementedException();

}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

SPSite site = properties.Feature.Parent as SPSite;

foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)

{

if (job.Title == TimerJobName)

{

job.Delete();

}

}

//throw new NotImplementedException();

}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)

{

//throw new NotImplementedException();

}

public override void FeatureUninstalling(SPFeatureReceiverProperties properties)

{

//throw new NotImplementedException();

}

 

ModifyTitle

public class ModifyTitle : SPJobDefinition

{

public ModifyTitle():base(){}

public ModifyTitle(string TimerName, SPWebApplication webapp) : base(TimerName, webapp, null, SPJobLockType.ContentDatabase)

{

//TimerJob的標題

this.Title = "定期修改Title的TimerJob";

}

public override void Execute(Guid targetInstanceId)

{

SPWebApplication webapp = this.Parent as SPWebApplication;

SPContentDatabase contentDB=webapp.ContentDatabases[targetInstanceId];

foreach (SPItem item in contentDB.Sites[0].RootWeb.Lists["TimerJob"].Items)

{

DateTime dt = Convert.ToDateTime(item["創建時間"].ToString());

item["標題"] = "今天是這個月的第" + dt.Day.ToString() + "天";

item.Update();

}

//base.Execute(targetInstanceId);

}

}

 

Feature.xml(Id是需要重新生成的Guid)

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

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

Id="f0c813e8-68e0-4ad2-82cd-292b1b7222cd"

Title="Modify Title Timer Job"

Description="Modify Title Timer Job"

Scope="Site"

Hidden="TRUE"

Version="1.0.0.0"

ReceiverAssembly="TimerJob, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f7436af6afb9480b"

ReceiverClass="TimerJob.ModifyTitleInstall">

</Feature>

 

添加結果:

clip_image003

 

運行結果:無論標題是什么,都改成今天是這個月的第N天。

clip_image005

 

添加配置文件:

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

<configuration>

<appSettings>

<add key="AAString" value="http://localhost"/>

</appSettings>

</configuration>

 

獲取配置文件:

string AAString = ConfigurationManager.AppSettings.Get("AAString");

注:配置文件格式不對的話,可能造成Timer服務啟動錯誤,所以,可以拷一個控制台程序debug下面的Consoleapp.exe.config文件,然后改成OWSTIMER.exe.config,然后放到12/bin(C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN)下就可以了

 

部署TimerJob腳本:

@echo off

SET TEMPLATE="c:\program files\common files\microsoft shared\web server extensions\12\Template"

Echo Copying files to TEMPLATES directory

xcopy /e /y 12\TEMPLATE\* %TEMPLATE%

Echo Copying TimerJob.dll to GAC

"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\gacutil.exe" -if bin\TimerJob.dll

iisreset

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin\stsadm" -o installfeature -filename TimerJob\feature.xml -force

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin\stsadm" -o deactivatefeature -filename TimerJob\feature.xml -url http://localhost -force

"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin\stsadm" -o activatefeature -filename TimerJob\feature.xml -url http://localhost -force

net stop SPTimerV3

net start SPTimerV3

PAUSE

注:新的TimerJob運行一定要重啟SPTimerV3服務,在windows服務里面,如下圖:

clip_image007

調試:TimerJob程序和WebPart等SharePoint程序,運行的進程不一樣,如果需要調試,需要重新安裝TimerJob,然后附加到SharePoint計時器進程(下圖),進行調試!

clip_image008

體會:

       開發完TimerJob感覺,和SharePoint的東西有一樣的特點,就是代碼開發比較簡單,但是雜七雜八的事情很多,部署、調試起來比較麻煩,而且非常需要細心,如果其間遇到各種bug,可以建議重啟下機器(我就是頭天晚上,各種報錯,轉天就好了)。

       還有就是,我的代碼是SharePoint2007環境開發的,如果在2010或者更高版本,代碼基本是類似的,注意目錄即可,部署方式可能需要PowerShell,可以網上查一下。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM