带有计时器作业的SP 2013 Webpart [英] SP 2013 Webpart with Timer Jobs
问题描述
您好,我是
我在sharepoint 2013下开发了一个webpart,我在其中插入了gridview和一个通过电子邮件发送的按钮,当我点击按钮时,我收到了一封电子邮件,内容是邮件有gridview,我想要一个解决方案我怎么办在计时器工作中$
我不想每天通过电子邮件点击扫描按钮
谢谢
您好,
根据您的描述,我的理解是您希望创建一个每天向您发送电子邮件的计时器作业。
我们应该在Visual Studio中创建一个计时器作业。
1,创建一个SharePoint 2010项目
2,创建一个类文件名"TimerJobApplication",代码如下:
class CustomTimerJob:SPJobDefinition
{
public CustomTimerJob()
:base()
{
}
public CustomTimerJob(string jobName,SPService service,SPServer server,SPJobLockType targetType)
:base(jobName,service,server, targetType)
{
}
public CustomTimerJob(string jobName,SPWebApplication webApplication)
:base(j obName,webApplication,null,SPJobLockType.ContentDatabase)
{
this.Title =" Custom Timer Job" ;;
}
public override void执行(Guid contentDbId){
            string from = string.Empty;
            string smtpAddress = string.Empty;
            string to =" rkswain@ubnsoft.com" ;;
            string subject =" Email Notification";
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; string body ="< h1>通过测试我的计时器工作申请发送电子邮件< / h1>" ;;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; SPSecurity.RunWithElevatedPrivileges(delegate(){
SPWebApplication webApplication = this .Parent为SPWebApplication; SPContentDatabase contentDb = webApplication.ContentDatabases [contentDbId] ; SPWeb rootWeb = contentDb.Sites [0] .RootWeb;
SPList listjob = rootWeb.Lists.TryGetList("任务"); from = rootWeb。 Site.WebApplication.OutboundMailSenderAddress;
&NBSP; smtpAddress = rootWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
&NBSP;&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; bool emailSent = SendMail(smtpAddress,subject,body,true,from,to,null,null);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if(listjob!= null&& emailSent){
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; SPListItem newListItem = listjob.Items.Add();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; newListItem [" Title"] = string.Concat(" Email Notification Sent at:",DateTime.Now.ToString());
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; newListItem.Update();&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; });&NBSP;
&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; public bool SendMail(string smtpAddress,string subject,string body,bool isBodyHtml,string from,string to,string cc,string bcc){
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; bool mailSent = false;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; SmtpClient smtpClient = null;
&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;试试{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; smtpClient = new SmtpClient();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; smtpClient.Host = smtpAddress;
&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; MailMessage mailMessage = new MailMessage(from,to,subject,body);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if(!String.IsNullOrEmpty(cc)){
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; MailAddress CCAddress = new MailAddress(cc);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; mailMessage.CC.Add(CCAddress);&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }&NBSP;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if(!String.IsNullOrEmpty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BSP;&NBSP; return mailSent;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }&NBSP;
}3,创建一个Feature文件并将范围设置为site,代码如下:
public class Feature1EventReceiver:SPFeatureReceiver
{
const string JobName =" Custom Timer Job" ;;
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
DeleteJob(site); //如果已存在则删除作业
CreateJob(site); //创建新作业
}
private static void DeleteJob(SPSite site)
{
foreach(site.WebApplication.JobDefinitions中的SPJobDefinition作业)
if(job.Name == JobName)
job.Delete();
}
private static void CreateJob(SPSite site)
{
CustomTimerJob job = new CustomTimerJob(JobName,site.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 5;
schedule.Interval = 5;
job.Schedule = schedule;
job.Update();
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
DeleteJob(properties.Feature.Parent as SPSite); //删除作业
}
}
4,部署并调试它
如需了解更多信息,请查看以下链接:
http://sharepoint-kings.blogspot.sg/2013/02/create-custom-timer-job-in-sharepoint。 html
最诚挚的问候,
Andy Wu
Hello,
I developed a webpart under sharepoint 2013 in which I inserted a gridview and a button send by email when I click on the button I receive an email in the contents of the mail there gridview, I want a solution how I do In a timer job
I do not want to click the sweep button by email every day
Thank you
Hi,
According to your description, my understanding is that you want to create a timer job that send email to you every day.
We should create a Timer Job in Visual Studio.
1, Create a SharePoint 2010 project
2, Create a Class file name "TimerJobApplication", the code as below:
class CustomTimerJob : SPJobDefinition { public CustomTimerJob() : base() { } public CustomTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType) : base(jobName, service, server, targetType) { } public CustomTimerJob(string jobName, SPWebApplication webApplication) : base(jobName, webApplication, null, SPJobLockType.ContentDatabase) { this.Title = "Custom Timer Job"; } public override void Execute(Guid contentDbId) {
string from = string.Empty;
string smtpAddress = string.Empty;
string to = "rkswain@ubnsoft.com";
string subject = "Email Notification";
string body = "<h1> Email Sending from Testing My Timer Job Application </h1>";
SPSecurity.RunWithElevatedPrivileges(delegate() {
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
SPWeb rootWeb = contentDb.Sites[0].RootWeb;
SPList listjob = rootWeb.Lists.TryGetList("Tasks");
from = rootWeb.Site.WebApplication.OutboundMailSenderAddress;
smtpAddress = rootWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address;
bool emailSent = SendMail(smtpAddress, subject, body, true, from, to, null, null);
if (listjob != null && emailSent) {
SPListItem newListItem = listjob.Items.Add();
newListItem["Title"] = string.Concat("Email Notification Sent at : ", DateTime.Now.ToString());
newListItem.Update();
}
});
}
public bool SendMail(string smtpAddress, string subject, string body, bool isBodyHtml, string from, string to, string cc, string bcc) {
bool mailSent = false;
SmtpClient smtpClient = null;
try {
smtpClient = new SmtpClient();
smtpClient.Host = smtpAddress;
MailMessage mailMessage = new MailMessage(from, to, subject, body);
if (!String.IsNullOrEmpty(cc)) {
MailAddress CCAddress = new MailAddress(cc);
mailMessage.CC.Add(CCAddress);
}
if (!String.IsNullOrEmpty(bcc)) {
MailAddress BCCAddress = new MailAddress(bcc);
mailMessage.Bcc.Add(BCCAddress);
}
mailMessage.IsBodyHtml = isBodyHtml;
smtpClient.Send(mailMessage);
mailSent = true;
} catch (Exception) {
mailSent = false;
}
return mailSent;
} }3, Create a Feature file and set scope to site, the code as below:
public class Feature1EventReceiver : SPFeatureReceiver { const string JobName = "Custom Timer Job"; public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite site = properties.Feature.Parent as SPSite; DeleteJob(site); // Delete Job if already Exists CreateJob(site); // Create new Job } private static void DeleteJob(SPSite site) { foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) if (job.Name == JobName) job.Delete(); } private static void CreateJob(SPSite site) { CustomTimerJob job = new CustomTimerJob(JobName, site.WebApplication); SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; schedule.EndSecond = 5; schedule.Interval = 5; job.Schedule = schedule; job.Update(); } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { DeleteJob(properties.Feature.Parent as SPSite); // Delete the Job } }4, Deploy it and debug it
For more information, please check this link:
http://sharepoint-kings.blogspot.sg/2013/02/create-custom-timer-job-in-sharepoint.html
Best Regards,
Andy Wu
这篇关于带有计时器作业的SP 2013 Webpart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!