创建一个每月运行一次的计时器作业,它将sharepoint列表项导出到excel并存储在文档库中

本文关键字:列表 excel 文档 sharepoint 存储 运行 一个 一次 创建 作业 计时器 | 更新日期: 2023-09-27 18:19:18

我想创建一个每月运行一次的定时器作业或工作流,并将sharepoint列表数据导出到excel并将此文件存储在文档库中。

我已经下载了代码来创建定时器作业从下面的链接,但不知道如何包括上述要求http://code.msdn.microsoft.com/sharepoint - 2010 -自定义- 416 cd3a1

//Create class derived from SPJonDefinition Class 
 class ListTimerJob : SPJobDefinition 
    { 
         public ListTimerJob() 
            : base() 
        { 
        } 
        public ListTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType) 
            : base(jobName, service, server, targetType) 
        { 
        } 
        public ListTimerJob(string jobName, SPWebApplication webApplication) 
            : base(jobName, webApplication, null, SPJobLockType.ContentDatabase) 
        { 
            this.Title = "List Timer Job"; 
        } 
        public override void Execute(Guid contentDbId) 
        { 
            // get a reference to the current site collection's content database 
            SPWebApplication webApplication = this.Parent as SPWebApplication; 
            SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId]; 
            // get a reference to the "ListTimerJob" list in the RootWeb of the first site collection in the content database 
            SPList Listjob = contentDb.Sites[0].RootWeb.Lists["ListTimerJob"]; 
            // create a new list Item, set the Title to the current day/time, and update the item 
            SPListItem newList = Listjob.Items.Add(); 
            newList["Title"] = DateTime.Now.ToString(); 
            newList.Update(); 
        } 
} 
//Add Event receiver at Feature Level  
[Guid("9a724fdb-e423-4232-9626-0cffc53fb74b")] 
public class Feature1EventReceiver : SPFeatureReceiver 
    { 
        const string List_JOB_NAME = "ListLogger"; 
        // Uncomment the method below to handle the event raised after a feature has been activated. 
        public override void FeatureActivated(SPFeatureReceiverProperties properties) 
        { 
            SPSite site = properties.Feature.Parent as SPSite; 
            // make sure the job isn't already registered 
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) 
            { 
                if (job.Name == List_JOB_NAME) 
                    job.Delete(); 
            } 
            // install the job 
            ListTimerJob listLoggerJob = new ListTimerJob(List_JOB_NAME, site.WebApplication); 
            SPMinuteSchedule schedule = new SPMinuteSchedule(); 
            schedule.BeginSecond = 0; 
            schedule.EndSecond = 59; 
            schedule.Interval = 5; 
            listLoggerJob.Schedule = schedule; 
            listLoggerJob.Update(); 
        } 
        // Uncomment the method below to handle the event raised before a feature is deactivated. 
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
        { 
            SPSite site = properties.Feature.Parent as SPSite; 
            // delete the job 
            foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) 
            { 
                if (job.Name == List_JOB_NAME) 
                    job.Delete(); 
            } 
}  

创建一个每月运行一次的计时器作业,它将sharepoint列表项导出到excel并存储在文档库中

我还建议您不要使用SharePoint定时器作业引擎。它肯定不稳定。有时作业根本不trigger,它们很难实例化,而且速度很慢。

当然,你总是可以花时间调整SharePoint来实现稳定性,但没有保证。我知道这听起来很重要,但是相信我,我不记得这台发动机的所有问题,但我们在它上面浪费了很多时间。

我推荐你Quartz。NETWindows Scheduler,如前所述。这些都是行之有效的解决方案,被很多人使用,也适用于SharePoint。

我们实现了Quartz。在我的公司,我们所有的定时器工作都在这个引擎上运行。我们已经两年没出过故障了。

问好。

您应该将spminutesschedule更改为SPMonthlyByDaySchedule,参见http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spschedule.aspx。

但是,我建议使用windows服务器调度程序和控制台应用程序。易于更改,易于维护(不需要重置!!),并且易于记录所有内容。我们将控制台应用程序用于各种预定作业,从1小时到1天不等。