环境:sharepoint 2010 + vs2010
测试:写一个定时发邮件的功能测试定时提醒功能
参考:http://www.cnblogs.com/isnull/archive/2011/05/05/2038309.html
开始了
步骤一:
- 创建一个asp.net网站项目
- 项目-->属性-->应用程序-->选择framework3.5
- 创建一个发邮件的类(TimerExecute)并测试
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Net.Mail; namespace SharepointStudy { public class TimerExecute { public void SendMail() { //写其他的业务代码也可以,如获取某个文档库中的记录,符合条件就发邮件
MailMessage message = new MailMessage(); message.From = new MailAddress(xxx@126.com); message.To.Add(xxx@123.com); message.Subject = "test"; message.Body = "testbody"; SmtpClient client = new SmtpClient("192.168.0.1"); client.Send(message); } } }
步骤二:创建一个自己的任务提醒类,该类继承SPJobDefinition,按我的理解就是一个sharepoint自带的job,该类调用上面的业务类
你创建好了再把它部署上去,他就会按定义好的规则执行(规则可以部署好后再到管理中心里修改),
具体位置(管理中心--监控--计时器作业(复查作业定义))
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.SharePoint.Administration; namespace SharepointStudy { public class TaskListTimerJob :SPJobDefinition { public TaskListTimerJob() : base() { } public TaskListTimerJob(string _timername, SPWebApplication _wp) : base(_timername, _wp, null, SPJobLockType.ContentDatabase) { this.Title = _timername; } public override void Execute(Guid targetInstanceId) { //base.Execute(targetInstanceId); try { new TimerExecute().SendMail(); } catch (Exception ex) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter("c:\timerjob.txt", true)) { sw.WriteLine("source=" + ex.Source + " Message=" + ex.Message); } } } } }
步骤三:
把上面工程的dll拷到C:\WINDOWS\assembly 下,如果考不进去.
项目必须强命名(http://www.cnblogs.com/xx_cs/archive/2011/07/12/2104019.html)
请拷贝时须管理员身份(域帐号且在administrator组也不行),但以管理员身份在vs2010命令提示中运行gacutil -i d:\Demo.dll(我把dll拷贝到d盘的根下了)可以安装成功
步骤四:发布job,写一个winform程序来发布
我理解为创建一个新的任务(TaskListTimerJob),该任务执行了业务类(timerexecute的sendmail方法),发布时须定义任务的执行规则(多久执行一次)
主演:
1:还是要用framework3.5
2:生成的目标平台选x64(http://www.byywee.com/page/M0/S238/238329.html),否则找不站点.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using SharepointStudy; namespace TimerJobSetUp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { SPSite site = new SPSite("http://gwsps03"); SPWebApplication web = site.WebApplication; foreach (SPJobDefinition job in web.JobDefinitions) { string name = job.Title; if (name == "MyCustomJobSendTaskMail") { job.Delete(); } } TaskListTimerJob newJob = new TaskListTimerJob("MyCustomJobSendTaskMail", site.WebApplication); //SPSchedule schedule = SPSchedule.FromString("daily at 20:46:00"); SPSchedule schedule = SPSchedule.FromString("every 15 minutes"); newJob.Schedule = schedule; newJob.Update(); //Console.WriteLine("部署成功"); MessageBox.Show("部署成功"); } catch (Exception ex) { MessageBox.Show(ex.Message); //Console.Write(ex.Message); } } } }
步骤五:运行程序,部署成功后就可以到管理中心--监控--计时器作业(复查作业定义))看到有一个你发布的job了
然后你就会每隔15分钟都收到一封垃圾邮件了