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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

步步为营 SharePoint 开发学习笔记系列 七、SharePoint Timer Job 开发

發布時間:2025/3/8 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 步步为营 SharePoint 开发学习笔记系列 七、SharePoint Timer Job 开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

概要

?? 項目需求要求我們每天晚上同步員工的一些信息到sharepoint 的user List ,我們決定定制開發sharepoint timer Job,Sharepoint timer Job是sharePoint的定時作業Job,需要安裝、布曙到服務器上,而這里我只是介紹下Job開發的例子,以供大家學習用。

開發設計

我們需要新建兩個類,TaskLoggerJob和TaskLoggerFeature,TaskLoggerJob實現這個Job具體做哪些工和,TaskLoggerFeature實現安裝和卸載這個Job以及定義Job執行時間和方式。

在開發Job時需要引用如下Dll

using Microsoft.SharePoint; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.Administration;

TaskLoggerJob設計代碼如下:

public class TaskLoggerJob : SPJobDefinition{#region [Fields]#endregion#region [Constructors]/// <summary>/// Initializes a new instance of the TaskLoggerJob class./// </summary>public TaskLoggerJob(): base(){}/// <summary>/// Initializes a new instance of the TaskLoggerJob class./// </summary>/// <param name="jobName">Name of the job.</param>/// <param name="service">The service.</param>/// <param name="server">The server.</param>/// <param name="targetType">Type of the target.</param>public TaskLoggerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType): base(jobName, service, server, targetType){}/// <summary>/// Initializes a new instance of the TaskLoggerJob class./// </summary>/// <param name="jobName">Name of the job.</param>/// <param name="webApplication">The web application.</param>public TaskLoggerJob(string jobName, SPWebApplication webApplication): base(jobName, webApplication, null, SPJobLockType.Job){this.Title = "Task Logger";}#endregion#region [Public Methods]/// <summary>/// Executes the specified content db id./// </summary>/// <param name="contentDbId">The content db id.</param>public override void Execute(Guid contentDbId){try{// get a reference to the current site collection's content databaseSPWebApplication webApplication = this.Parent as SPWebApplication;SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content databaseSPList taskList = contentDb.Sites[0].RootWeb.Lists["Tasks"];// create a new task, set the Title to the current day/time, and update the itemSPListItem newTask = taskList.Items.Add();newTask["Title"] = DateTime.Now.ToString();newTask.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}#endregion#region [Private Methods]#endregion}

在TaskLoggerFeature時我們調用這個構造方法:

/// <summary>/// Initializes a new instance of the TaskLoggerJob class./// </summary>/// <param name="jobName">Name of the job.</param>/// <param name="webApplication">The web application.</param>public TaskLoggerJob(string jobName, SPWebApplication webApplication): base(jobName, webApplication, null, SPJobLockType.Job){this.Title = "Task Logger";}

來初始化SPJobDefinition方法,Job具體要做的事性我們實現這個方法:

/// <summary>/// Executes the specified content db id./// </summary>/// <param name="contentDbId">The content db id.</param>public override void Execute(Guid contentDbId){try{// get a reference to the current site collection's content databaseSPWebApplication webApplication = this.Parent as SPWebApplication;SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];// get a reference to the "Tasks" list in the RootWeb of the first site collection in the content databaseSPList taskList = contentDb.Sites[0].RootWeb.Lists["Tasks"];// create a new task, set the Title to the current day/time, and update the itemSPListItem newTask = taskList.Items.Add();newTask["Title"] = DateTime.Now.ToString();newTask.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}

在這個方法里我們可以同事實現很多任務,而我們這里只是改變了它的title。

下面我們來講解TaskLoggerFeature的代碼設計,首先引用:

using Microsoft.SharePoint; using Microsoft.SharePoint.Administration;

而后代碼如下:

public class TaskLoggerFeature : SPFeatureReceiver{#region [Override Methods]/// <summary>/// Active the feature/// </summary>/// <param name="properties"></param>public override void FeatureActivated(SPFeatureReceiverProperties properties){SPSite site = properties.Feature.Parent as SPSite;SPSite currentSite = null;try{SPSecurity.RunWithElevatedPrivileges(delegate{currentSite = new SPSite(site.Url);});this.InstallTaskLoggerJob(currentSite);}catch (Exception ex){LogHepler.InitConfigListSiteUrl(site.Url);LogHepler.LogToShrepointList(ex);}finally{if (currentSite != null){currentSite.Dispose();}}}/// <summary>/// Deactive the feature/// </summary>/// <param name="properties"></param>public override void FeatureDeactivating(SPFeatureReceiverProperties properties){SPSite site = properties.Feature.Parent as SPSite;SPSite currentSite = null;try{SPSecurity.RunWithElevatedPrivileges(delegate{currentSite = new SPSite(site.Url);});SPWebApplication webApp = currentSite.WebApplication;this.UninstallTaskLoggerJob(webApp);}catch (Exception ex){LogHepler.InitConfigListSiteUrl(site.Url);LogHepler.LogToShrepointList(ex);}finally{if (currentSite != null){currentSite.Dispose();}}}/// <summary>/// Method that is executed when the feature end the installation/// </summary>/// <param name="properties"></param>public override void FeatureInstalled(SPFeatureReceiverProperties properties){}/// <summary>/// Method that is executed when the feature is unistalled/// </summary>/// <param name="properties"></param>public override void FeatureUninstalling(SPFeatureReceiverProperties properties){}#endregion#region [Private Methods]/// <summary>/// method to install the job/// </summary>/// <param name="web"></param>private void InstallTaskLoggerJob(SPSite site){TaskLoggerJob jobDef = new TaskLoggerJob("TaskLoggerJob", site.WebApplication);jobDef.Title = "TaskLoggerJob";jobDef.Properties.Add("SiteUrl", site.Url);this.InstallDayJob(jobDef, site, 23);//this.InstallHourJob(jobDef, site, 2);//this.InstallMinuteJob(jobDef, site, 10, 10);}/// <summary>/// Method to unistall a job/// </summary>/// <param name="web">The SPWeb where need to remove the job</param>private void UninstallTaskLoggerJob(SPWebApplication webApp){try {SPJobDefinitionCollection jobColl = webApp.JobDefinitions;if (jobColl != null){List<Guid> idsToRemove = new List<Guid>();foreach (SPJobDefinition jobDef in jobColl){if (!String.IsNullOrEmpty(jobDef.Title) && jobDef.Title.StartsWith("TaskLoggerJob")){idsToRemove.Add(jobDef.Id);}}if (idsToRemove.Count > 0){foreach (Guid gd in idsToRemove){jobColl.Remove(gd);}}}}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}/// <summary>/// Method to install the job that will execute by hour/// </summary>/// <param name="jobDef">The JobDefinition to apply</param>/// <param name="web">The SPWeb that will execute the job</param>/// <param name="minute">The minute to start the job in that hour</param>private void InstallDayJob(SPJobDefinition jobDef, SPSite site, int hour){try{SPWebApplication webApp = site.WebApplication;SPJobDefinitionCollection jboColl = webApp.JobDefinitions;SPDailySchedule daySched = new SPDailySchedule();daySched.BeginHour = hour;daySched.BeginMinute = 0;daySched.BeginSecond = 0;daySched.EndHour = hour;daySched.EndMinute = 0;daySched.EndSecond = 0;jobDef.Schedule = daySched;SPJobDefinition oldJob = this.GetJobDeffinition(jobDef.Title, jboColl);if (oldJob != null){jboColl.Remove(oldJob.Id);webApp.Update();}jboColl.Add(jobDef);webApp.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}/// <summary>/// Method to install the job that will execute by hour/// </summary>/// <param name="jobDef">The JobDefinition to apply</param>/// <param name="web">The SPWeb that will execute the job</param>/// <param name="minute">The minute to start the job in that hour</param>private void InstallHourJob(SPJobDefinition jobDef, SPSite site, int minute){try{SPWebApplication webApp = site.WebApplication;SPJobDefinitionCollection jboColl = webApp.JobDefinitions;SPHourlySchedule hourSched = new SPHourlySchedule();hourSched.BeginMinute = minute;jobDef.Schedule = hourSched;SPJobDefinition oldJob = this.GetJobDeffinition(jobDef.Title, jboColl);if (oldJob != null){jboColl.Remove(oldJob.Id);webApp.Update();}jboColl.Add(jobDef);webApp.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}/// <summary>/// Method to install the job that will execute by minute/// </summary>/// <param name="jobDef">The JobDefinition to apply</param>/// <param name="web">The SPWeb that will execute the job</param>/// <param name="secound">The seconds to start the job in that minute</param>private void InstallMinuteJob(SPJobDefinition jobDef, SPSite site, int second, int interval){try{SPWebApplication webApp = site.WebApplication;SPJobDefinitionCollection jboColl = webApp.JobDefinitions;SPMinuteSchedule minSched = new SPMinuteSchedule();minSched.Interval = interval;minSched.BeginSecond = second;jobDef.Schedule = minSched;SPJobDefinition oldJob = this.GetJobDeffinition(jobDef.Title, jboColl);if (oldJob != null){jboColl.Remove(oldJob.Id);webApp.Update();}jboColl.Add(jobDef);webApp.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}/// <summary>/// Get the JobDefinition to install or remove/// </summary>/// <param name="Title">Title of the job</param>/// <param name="jobCollection">The JobCollection to find the job</param>/// <returns>JbDefinition that found in this collection</returns>private SPJobDefinition GetJobDeffinition(string Title, SPJobDefinitionCollection jobCollection){SPJobDefinition result = null;if (jobCollection != null){foreach (SPJobDefinition job in jobCollection){if (job.Title.Equals(Title)){result = job;break;}}}return result;}#endregion}

下面這個方法是激活這個Job的feature,在sharepoint里每一個Job都有一個feature來講行實現,它會生成相應的feature的xml方件:

/// <summary>/// Active the feature/// </summary>/// <param name="properties"></param>public override void FeatureActivated(SPFeatureReceiverProperties properties){SPSite site = properties.Feature.Parent as SPSite;SPSite currentSite = null;try{SPSecurity.RunWithElevatedPrivileges(delegate{currentSite = new SPSite(site.Url);});this.InstallTaskLoggerJob(currentSite);}catch (Exception ex){LogHepler.InitConfigListSiteUrl(site.Url);LogHepler.LogToShrepointList(ex);}finally{if (currentSite != null){currentSite.Dispose();}}} ? ? 卸載這個Job的方法如下: /// <summary>/// Deactive the feature/// </summary>/// <param name="properties"></param>public override void FeatureDeactivating(SPFeatureReceiverProperties properties){SPSite site = properties.Feature.Parent as SPSite;SPSite currentSite = null;try{SPSecurity.RunWithElevatedPrivileges(delegate{currentSite = new SPSite(site.Url);});SPWebApplication webApp = currentSite.WebApplication;this.UninstallTaskLoggerJob(webApp);}catch (Exception ex){LogHepler.InitConfigListSiteUrl(site.Url);LogHepler.LogToShrepointList(ex);}finally{if (currentSite != null){currentSite.Dispose();}}}

?

Job的執行時間可以按分、時、天、月、年來執行可以進行如下定義,分、時、天。概據你的需要來執行。

/// <summary>/// Method to install the job that will execute by hour/// </summary>/// <param name="jobDef">The JobDefinition to apply</param>/// <param name="web">The SPWeb that will execute the job</param>/// <param name="minute">The minute to start the job in that hour</param>private void InstallDayJob(SPJobDefinition jobDef, SPSite site, int hour){try{SPWebApplication webApp = site.WebApplication;SPJobDefinitionCollection jboColl = webApp.JobDefinitions;SPDailySchedule daySched = new SPDailySchedule();daySched.BeginHour = hour;daySched.BeginMinute = 0;daySched.BeginSecond = 0;daySched.EndHour = hour;daySched.EndMinute = 0;daySched.EndSecond = 0;jobDef.Schedule = daySched;SPJobDefinition oldJob = this.GetJobDeffinition(jobDef.Title, jboColl);if (oldJob != null){jboColl.Remove(oldJob.Id);webApp.Update();}jboColl.Add(jobDef);webApp.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}/// <summary>/// Method to install the job that will execute by hour/// </summary>/// <param name="jobDef">The JobDefinition to apply</param>/// <param name="web">The SPWeb that will execute the job</param>/// <param name="minute">The minute to start the job in that hour</param>private void InstallHourJob(SPJobDefinition jobDef, SPSite site, int minute){try{SPWebApplication webApp = site.WebApplication;SPJobDefinitionCollection jboColl = webApp.JobDefinitions;SPHourlySchedule hourSched = new SPHourlySchedule();hourSched.BeginMinute = minute;jobDef.Schedule = hourSched;SPJobDefinition oldJob = this.GetJobDeffinition(jobDef.Title, jboColl);if (oldJob != null){jboColl.Remove(oldJob.Id);webApp.Update();}jboColl.Add(jobDef);webApp.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}/// <summary>/// Method to install the job that will execute by minute/// </summary>/// <param name="jobDef">The JobDefinition to apply</param>/// <param name="web">The SPWeb that will execute the job</param>/// <param name="secound">The seconds to start the job in that minute</param>private void InstallMinuteJob(SPJobDefinition jobDef, SPSite site, int second, int interval){try{SPWebApplication webApp = site.WebApplication;SPJobDefinitionCollection jboColl = webApp.JobDefinitions;SPMinuteSchedule minSched = new SPMinuteSchedule();minSched.Interval = interval;minSched.BeginSecond = second;jobDef.Schedule = minSched;SPJobDefinition oldJob = this.GetJobDeffinition(jobDef.Title, jboColl);if (oldJob != null){jboColl.Remove(oldJob.Id);webApp.Update();}jboColl.Add(jobDef);webApp.Update();}catch (Exception ex){LogHepler.LogToShrepointList(ex);}}

?

在完成了上面的代碼設計后,我們接著就需要把Job布曙到服務器中。

要以上代碼生成Windows SharePoint Solution Package (*.WSP) 來布曙。

步驟如下:

一、首先進入sharePoint Central administrator v3 管理頁面,選擇Operation下的Solution Management

二、檢索TaskLoggerJob.wsp

如果以前安裝過這個Job先要卸載,再安裝。?
三、執行命令?? stsadm -o addsolution -filename "TaskLoggerJob.wsp"? 添加Job的solution

四、執行命令 stsadm -o deactivatefeature -name TaskLoggerJob -url http://[site]/
????? 而后再執行命令? stsadm -o execadmsvcjobs
五、執行命令 stsadm -o activatefeature -name TaskLoggerJob -url http://[site]/
????? 而后再執行命令? stsadm -o execadmsvcjobs

總結

sharepoint timer job是用來完成系統定里執行的一此任務,是由這個進程完成的OWSTIMER.EXE .

作者:spring yang

出處:http://www.cnblogs.com/springyangwc/

本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。

轉載于:https://www.cnblogs.com/springyangwc/archive/2011/07/25/2115963.html

總結

以上是生活随笔為你收集整理的步步为营 SharePoint 开发学习笔记系列 七、SharePoint Timer Job 开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 中国1级毛片 | 国产精品一二三区在线观看 | 欧美无砖区| 美女毛片在线 | 一级特黄aa大片免费播放 | 国产区一区二区三 | 午夜国产小视频 | 国产嫩bbwbbw高潮 | 黑人玩弄人妻一区二区三区免费看 | 国产高清视频在线免费观看 | 黄色一级一片免费播放 | 国产xx视频 | 神马香蕉久久 | 午夜肉伦伦 | 欧美大片xxxx| 岛国免费视频 | 天天射天天干天天操 | 人人插人人干 | 久久久久高清 | 欧美日韩1 | 亚洲AV成人无码久久 | 青青青视频在线 | 黄色片在线免费观看 | 超碰综合网 | 日本一区视频在线播放 | 亚洲av永久无码精品国产精品 | 国内三级视频 | 在线看一区二区 | 90岁老太婆乱淫 | 寡妇激情做爰呻吟 | 亚洲一二三四五 | 免费精品在线视频 | 一边摸一边抽搐一进一出视频 | 自拍偷拍av | 黄色视屏在线播放 | 日本三级中国三级99人妇网站 | 极品少妇xxxx精品少妇偷拍 | 亚洲人免费 | 国产精品96| 欧美色综合色 | 亚洲欧美一区在线 | 一区精品视频在线观看 | 污污视频在线看 | 免费麻豆av | 天天黄色片 | 艳妇臀荡乳欲伦交换在线播放 | 视频在线观看免费大片 | 毛片内射久久久一区 | 欧美精品123区 | 麻豆精品免费观看 | 中文字幕亚洲一区二区三区 | 色久阁 | 三级中文字幕在线 | 三级91 | 国产精品国产三级国产在线观看 | 少妇精品无码一区二区 | 久久精品中文闷骚内射 | 1000部拍拍拍18勿入免费视频 | 日日射av | 射久久| 中文字幕日本一区 | 日日日操操操 | 99热这里都是精品 | 国产亚洲av片在线观看18女人 | 樱桃视频污污 | 91色偷偷 | 九九色影院| 四虎免费久久 | 挪威xxxx性hd极品 | 亚洲精选一区 | 成人综合一区 | 日韩在线观看精品 | 国产一级黄色录像 | 日韩激情网址 | 欧美亚洲黄色 | 成人做爰免费视频免费看 | 精品视频一区二区三区四区五区 | 国产成人欧美一区二区三区91 | 天码人妻一区二区三区在线看 | 男人操女人动态图 | 粉嫩av一区二区白浆 | 国产v综合v亚洲欧美久久 | 欧美国产高清 | 人人爽人人爽人人片 | 最新av网站在线观看 | 国产一区影院 | 日本孕妇孕交 | 91香蕉一区二区三区在线观看 | 欧美久久综合 | 老司机精品福利视频 | 精品欧美一区二区在线观看 | 亚洲小说区图片区 | 在线视频观看一区 | 国产农村妇女精品一区 | 成人h动漫精品一区二区 | 亚洲一区动漫 | 麻豆综合 | 91高跟黑色丝袜呻吟动态图 | 深夜免费视频 |