[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
public class TimerJobFeatureEventReceiver : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
if (webApplication != null)
{
DeleteJobAndSettings(properties);
try
{
CustomTimerJob job = new CustomTimerJob(webApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 5;
job.Schedule = schedule;
job.Update();
}
catch (Exception ex)
{
CustomLoggingSvcBase.Current.LogException(CustomLoggingSvcBase.DiagnosticCategory.TimerJob, ex);
throw new SPException(ex.Message, ex);
}
}
else
{
string msg = "FeatureActivated - WebApplication is null";
throw new SPException(msg);
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
DeleteJobAndSettings(properties);
}
#region Utils
private void DeleteJobAndSettings(SPFeatureReceiverProperties properties)
{
SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
if (webApplication != null)
{
foreach (SPJobDefinition job in webApplication.JobDefinitions)
{
if (job.Name.Equals(CustomTimerJob.JobName))
{
job.Delete();
break;
}
}
}
else
{
string msg = string.Format("{0}: DeleteJobAndSettings - WebApplication is null", this.GetType().Name);
throw new SPException(msg);
}
}
public static string GetFeatureGuid()
{
string ret = string.Empty;
Type t = typeof(TimerJobFeatureEventReceiver);
object[] attrs = t.GetCustomAttributes(typeof(GuidAttribute), false);
if (attrs != null && attrs.Length == 1)
{
GuidAttribute attr = attrs[0] as GuidAttribute;
if (attr != null)
{
ret = attr.Value;
}
}
return ret;
}
#endregion
Comments