Created
December 13, 2013 10:07
-
-
Save splessons/7942268 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Security.Permissions; | |
using Microsoft.SharePoint; | |
using Microsoft.SharePoint.Administration; | |
using customtimerjob.SPJobs; | |
namespace customtimerjob.Features.SPTimerjobFeature1 | |
{ | |
/// <summary> | |
/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade. | |
/// </summary> | |
/// <remarks> | |
/// The GUID attached to this class may be used during packaging and should not be modified. | |
/// </remarks> | |
[Guid("7d8c815c-b13d-4cbe-8fe9-4d0c2f5087ef")] | |
public class SPTimerjobFeature1EventReceiver : SPFeatureReceiver | |
{ | |
const string JobName = "SP Task Timer"; | |
// Uncomment the method below to handle the event raised after a feature has been activated. | |
public override void FeatureActivated(SPFeatureReceiverProperties properties) | |
{ | |
try | |
{ | |
SPSecurity.RunWithElevatedPrivileges(delegate() | |
{ | |
SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent; | |
SPSite site = properties.Feature.Parent as SPSite; | |
DeleteExistingJob(JobName, parentWebApp); | |
CreateJob(parentWebApp); | |
}); | |
} | |
catch(Exception ex){ | |
throw ex; | |
} | |
} | |
// Uncomment the method below to handle the event raised before a feature is deactivated. | |
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) | |
{ | |
lock (this) | |
{ | |
try | |
{ | |
SPSecurity.RunWithElevatedPrivileges(delegate() | |
{ | |
SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent; | |
DeleteExistingJob(JobName, parentWebApp); | |
}); | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
} | |
private bool CreateJob(SPWebApplication site) | |
{ | |
bool jobCreated = false; | |
try | |
{ | |
SPTImerJobClass job = new SPTImerJobClass(JobName, site); | |
SPMinuteSchedule schedule = new SPMinuteSchedule(); | |
schedule.BeginSecond = 0; | |
schedule.EndSecond = 59; | |
schedule.Interval = 15; | |
job.Schedule = schedule; | |
job.Update(); | |
} | |
catch (Exception) | |
{ | |
return jobCreated; | |
} | |
return jobCreated; | |
} | |
public bool DeleteExistingJob(string jobName, SPWebApplication site) | |
{ | |
bool jobDeleted = false; | |
try | |
{ | |
foreach (SPJobDefinition job in site.JobDefinitions) | |
{ | |
if (job.Name == jobName) | |
{ | |
job.Delete(); | |
jobDeleted = true; | |
} | |
} | |
} | |
catch (Exception) | |
{ | |
return jobDeleted; | |
} | |
return jobDeleted; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment