首页 > 代码库 > Share Point 创建 TimerJob
Share Point 创建 TimerJob
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())); } }); } } }
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(); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。