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(); } }
Share Point 创建 TimerJob,布布扣,bubuko.com
原文:http://www.cnblogs.com/nanfei/p/3709283.html