码迷,mamicode.com
首页 > 其他好文 > 详细

Share Point 创建 TimerJob

时间:2014-05-08 22:30:30      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   tar   

bubuko.com,布布扣
public class SyncMetadataJob:SPJobDefinition 
    {
        private const string JobName = @"Metadata Sync Job";
        private int counter = 0;

        public SyncMetadataJob() : base() { }

        public SyncMetadataJob(string jobName, SPService service)
            : base(jobName, service, null, SPJobLockType.ContentDatabase) {
                this.Title = JobName;
        }

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

        public override void Execute(Guid targetInstanceId) {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    counter = 0;
                    Log("Sync Metadata Job Started.");
                    var lastSyncDate = getLastSyncDate();
                    syncMetaData(lastSyncDate);
                    updateLastSyncDate();
                    Log(string.Format("Sync Metadata {0} terms Updated.", counter));
                    Log("Sync Metadata Job Completed.");
                }
                catch (Exception ex)
                {
                    Log(string.Format("Sync Metadata Job thrown an exception:{0}.", ex.ToString()));
                }
                
            });
        }
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            DeleteSyncMetadataJob(site);
            CreateSyncMetadataJob(site);
        }


        // 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;
            DeleteSyncMetadataJob(site);
        }

private void CreateSyncMetadataJob(SPSite site)
        {
            var syncMetadataJob = new SyncMetadataJob(metadataSyncJobName, site.WebApplication);
            var schedule = new SPMinuteSchedule();
            schedule.BeginSecond = 0;
            schedule.EndSecond = 59;
            schedule.Interval = 3; //60 * 24 * 3;    //per 3 days
            syncMetadataJob.Schedule = schedule;
            syncMetadataJob.Update();
        }

        private void DeleteSyncMetadataJob(SPSite site)
        {
            foreach (var job in site.WebApplication.JobDefinitions)
            {
                if (job.Name == metadataSyncJobName)
                    job.Delete();
            }
        }
bubuko.com,布布扣

 

Share Point 创建 TimerJob,布布扣,bubuko.com

Share Point 创建 TimerJob

标签:style   blog   class   code   java   tar   

原文地址:http://www.cnblogs.com/nanfei/p/3709283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!