您的位置:首页 > 其它

Sharepoint 2013 创建TimeJob 自动发送邮件

2015-08-05 17:25 141 查看
创建Time Job

继承继承SPJobDefinition 并且实现里边的 Execute方法

部署

可以手动部署,把程序集放到GAC,手动激活feature

如果部署的时候说feature已经存在,在feature的XML里面添加代码 AlwaysForceInstall="TRUE",如下

<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
AlwaysForceInstall="TRUE"
Title="Time Job for Find Document"
Description="Custom Feature for Time job">
</Feature>


安装

写到Feature,添加或者删除

更新

需要重新启动Windows SharePoint Services Timer 服务

调试

把OWSTIMER.EXE添加到进程

定义Timejob代码

引用

using Microsoft.SharePoint.Administration;

using Microsoft.SharePoint.Utilities;

public class FindDocument:SPJobDefinition
{
public FindDocument()
: base()
{
}

public FindDocument(string jobName, SPWebApplication webapp)
: base(jobName, webapp, null, SPJobLockType.ContentDatabase)
{
this.Title = Constants.FDJobName;
}

public override void Execute(Guid targetInstanceId)
{
SPWebApplication webapp = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webapp.ContentDatabases[targetInstanceId];

string webUrl = this.Properties["WebUrl"].ToString();
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
//Send mail to somebody
SPUtility.SendEmail(web, false, false,
"xxx@xxx.com", "E-mail title",
"E-mail body");
//udpate list
SPList list = web.Lists["Test"];
SPListItem item = list.Items[0];
item["Title"] = "Date " + DateTime.Now.ToString();
item.SystemUpdate();
}
}
}

//If active feature error, please active feature manually.
protected override bool HasAdditionalUpdateAccess()
{
return true;
}
}


安装卸载Timejob代码

引用

using Microsoft.SharePoint.Administration;

public class TimeJobFeatureEventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
const string JobName = Constants.FDJobName;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
DeleteJob(web); // Delete Job if already Exists
CreateJob(web); // Create new Job
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
DeleteJob(properties.Feature.Parent as SPWeb); // Delete the Job
}

private static void DeleteJob(SPWeb web)
{
foreach (SPJobDefinition job in web.Site.WebApplication.JobDefinitions)
if (job.Name == JobName)
job.Delete();
}

private static void CreateJob(SPWeb web)
{

TimeJob.Jobs.FindDocument job = new TimeJob.Jobs.FindDocument(JobName, web.Site.WebApplication);
job.Properties.Add("WebUrl", web.Url);
SPDailySchedule schedule = new SPDailySchedule();
schedule.BeginHour = 16;
schedule.BeginMinute = 00;
schedule.EndHour = 16;
schedule.EndMinute = 30;
job.Schedule = schedule;
job.Update();
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: