Sharepoint 2013 创建TimeJob 自动发送邮件

创建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,
                       "[email protected]", "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();
        }
    }

  

时间: 2024-08-03 21:00:10

Sharepoint 2013 创建TimeJob 自动发送邮件的相关文章

SharePoint 2013 创建web应用程序报错&quot;This page can’t be displayed&quot;

错误描述 This page can't be displayed ?Make sure the web address http://centeradmin is correct. ?Look for the page with your search engine. ?Refresh the page in a few minutes. 发生过程 创建web应用程序,填写完基本信息以后点击确定,就是"这不会花费很长时间"的提示,然后就报错了,并且SharePoint和iis的日志中

SharePoint 2013 创建我的站点、onedrive

SharePoint 2013 创建我的站点.onedrive 参考:https://technet.microsoft.com/zh-cn/library/ee624362.aspx 策略:https://technet.microsoft.com/zh-cn/library/cc262500.aspx 在管理中心中,单击"应用程序管理",然后单击"创建网站集". 注意指定配额.配置网站和锁定,就创建的是都有选项. 选 择当前站点80,录入一个标题URL 在&qu

SharePoint 2013 创建 Site Collection

在之前的文章中,通过SharePoint Central Administration 创建了Web Application.在这篇文章中将继续SharePoint 2013之旅——还是以Step By Step的形式演示如何在SharePoint 2013中创建Site Collection(网站集),首先梳理下知识点. Site Collection Site Collection是Sites(网站)的集合. Site Collection最大可以包含250000个Sites和Sub Sit

如何在SharePoint 2013 创建Power pivot 库(How to create a PowerPivot Gallery )及错误解决方法

如何在SharePoint 2013 创建Power pivot 库(How to create a PowerPivot Gallery ) 为了这样一个功能 浪费我一个上午的时间. 需求是在SharePoint2013 的环境上创建一个Power Point 库. 1,打开网站--网站设置--网站集管理--网站集功能. 激活 power pivot 网站集功能 如果找不到,英文版是 这里注意,如果没有这项就要重新配置power pivot 我的时间 就是浪费在这里的.  1.单击"开始&qu

sharepoint 2013 创建母版页

一.创建新的母版页, 并添加了新的样式表 1.从CodePlex 上获得Starter Master Pages for SharePoint 2010 或复制以下母版代码 <%@Master language="C#"%><%@Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft

利用SharePoint 2013 创建Service Desk 网站之利用SharePoint Designer设定邮件发送动作!

我们完成了所有的表单的发布之后,我们就可以根据表单创建我们的Service Ticket了,但是还有一个问题,我如何将这种变更邮件发送给我们的最终用户呢?这个就依靠我们的SharePoint Designer 来完成这一环了. 我们接下来就可以使用我们的Sharepoint Designer 来进行我们的邮件规则的推送.我们现在分析下我们发送邮件的情形,根据我们的不同的规则来定义不同的邮件发送内容. 接下来我们设定相应的规则: 接下来我们都根据不同的规则设定发送邮件: 接下来我们来写关于创建服务

SharePoint 2013 创建web应用程序报错&amp;quot;This page can’t be displayed&amp;quot;

错误描写叙述 This page can't be displayed ?Make sure the web address http://centeradmin is correct. ?Look for the page with your search engine. ?Refresh the page in a few minutes. 发生过程 创建web应用程序,填写完基本信息以后点击确定,就是"这不会花费非常长时间"的提示,然后就报错了,而且SharePoint和iis的

sharepoint 2013创建网站集,域帐户无法访问,只有administrator可以访问

解决方法: 1.创建WEB应用程序时,可配置帐户必须为域帐户 2.确定关闭防火墙(这是重点) 我在测试时发现80端口和30714端口在其它同事的电脑上(域帐户)都可以访问,除这两个端口以后都无法访问,出现以下错误,就是因为防火墙没关闭问题

SharePoint 2013 入门教程之创建及修改母版页

原文:SharePoint 2013 入门教程之创建及修改母版页 在SharePoint 2013中,微软提供了根据HTML页面转换Master页的方法,并支持单项同步,但是这样的更新,并不完善,会使一些功能造成丢失,所以,了解Master结构的人,尽量直接去修改Master比较方便. 当然,如果需要使用母版页,必须开启SharePoint 2013的发布功能,也就是去网站集功能和网站功能,打开激活发布的Feature. 1.激活发布功能 2.打开SharePoint Designer 2013