首页 > 其他 > 详细

Share Point 创建 TimerJob

时间:2014-05-08 22:28:21      阅读:399      评论:0      收藏:0      [点我收藏+]
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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!