日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SharePoint 开发TimerJob 介绍

發(fā)布時(shí)間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SharePoint 开发TimerJob 介绍 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

?????? TimerJob項(xiàng)目結(jié)構(gòu),主要有兩個(gè)Class,一個(gè)是用來定義TimerJob功能的,一個(gè)是用來部署開發(fā)好的TimerJob的,分別繼承兩個(gè)不同的類。如下圖,先建一個(gè)如下結(jié)構(gòu)的項(xiàng)目:

?

文件描述:

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

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

激活TimerJob的Feature.xml

添加強(qiáng)命名,因?yàn)閷砩傻膁ll是要放到GAC里面去的

?

添加引用:

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

using Microsoft.SharePoint;

using Microsoft.SharePoint.Administration;

?

ModifyTitleInstall

public class ModifyTitleInstall : SPFeatureReceiver

{

const string TimerJobName = "ModifyTitleTimerJob";//TimerJob的標(biāo)題

//激活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();//計(jì)時(shí)器對(duì)象

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的標(biāo)題

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["創(chuàng)建時(shí)間"].ToString());

item["標(biāo)題"] = "今天是這個(gè)月的第" + 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>

?

添加結(jié)果:

?

運(yùn)行結(jié)果:無論標(biāo)題是什么,都改成今天是這個(gè)月的第N天。

?

添加配置文件:

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

<configuration>

<appSettings>

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

</appSettings>

</configuration>

?

獲取配置文件:

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

注:配置文件格式不對(duì)的話,可能造成Timer服務(wù)啟動(dòng)錯(cuò)誤,所以,可以拷一個(gè)控制臺(tái)程序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運(yùn)行一定要重啟SPTimerV3服務(wù),在windows服務(wù)里面,如下圖:

調(diào)試:TimerJob程序和WebPart等SharePoint程序,運(yùn)行的進(jìn)程不一樣,如果需要調(diào)試,需要重新安裝TimerJob,然后附加到SharePoint計(jì)時(shí)器進(jìn)程(下圖),進(jìn)行調(diào)試!

體會(huì):

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

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

總結(jié)

以上是生活随笔為你收集整理的SharePoint 开发TimerJob 介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。