Advertisement
private775

C#/SP: Timer Job feature activation/deactivation

Jun 6th, 2016
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.78 KB | None | 0 0
  1.     [Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
  2.     public class TimerJobFeatureEventReceiver : SPFeatureReceiver
  3.     {
  4.  
  5.  
  6.         public override void FeatureActivated(SPFeatureReceiverProperties properties)
  7.         {
  8.  
  9.             SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
  10.             if (webApplication != null)
  11.             {
  12.                 DeleteJobAndSettings(properties);
  13.                 try
  14.                 {
  15.                     CustomTimerJob job = new CustomTimerJob(webApplication);
  16.                     SPMinuteSchedule schedule = new SPMinuteSchedule();
  17.                     schedule.BeginSecond = 0;
  18.                     schedule.EndSecond = 59;
  19.                     schedule.Interval = 5;
  20.                     job.Schedule = schedule;
  21.  
  22.                     job.Update();
  23.                 }
  24.                 catch (Exception ex)
  25.                 {
  26.                     CustomLoggingSvcBase.Current.LogException(CustomLoggingSvcBase.DiagnosticCategory.TimerJob, ex);
  27.                     throw new SPException(ex.Message, ex);
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 string msg = "FeatureActivated - WebApplication is null";
  33.                 throw new SPException(msg);
  34.             }
  35.  
  36.         }
  37.  
  38.         public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  39.         {
  40.             DeleteJobAndSettings(properties);
  41.         }
  42.  
  43.  
  44.         #region Utils
  45.  
  46.         private void DeleteJobAndSettings(SPFeatureReceiverProperties properties)
  47.         {
  48.             SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
  49.             if (webApplication != null)
  50.             {
  51.                 foreach (SPJobDefinition job in webApplication.JobDefinitions)
  52.                 {
  53.                     if (job.Name.Equals(CustomTimerJob.JobName))
  54.                     {
  55.                         job.Delete();
  56.                         break;
  57.                     }
  58.                 }
  59.             }
  60.             else
  61.             {
  62.                 string msg = string.Format("{0}: DeleteJobAndSettings - WebApplication is null", this.GetType().Name);
  63.                 throw new SPException(msg);
  64.             }
  65.  
  66.         }
  67.  
  68.         public static string GetFeatureGuid()
  69.         {
  70.             string ret = string.Empty;
  71.  
  72.             Type t = typeof(TimerJobFeatureEventReceiver);
  73.             object[] attrs = t.GetCustomAttributes(typeof(GuidAttribute), false);
  74.             if (attrs != null && attrs.Length == 1)
  75.             {
  76.                 GuidAttribute attr = attrs[0] as GuidAttribute;
  77.                 if (attr != null)
  78.                 {
  79.                     ret = attr.Value;
  80.                 }
  81.             }
  82.             return ret;
  83.         }
  84.         #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement