Hi there,
I worked on the small requirement to add the items for a custom list with predefined schedule.
Here we used the base class SPJobDefinition which is part of SharePoint, Administration namespace.
So we need to make sure to add the reference Microsoft.SharePoint.Administration reference and the namespace as well.
our custom timer job class needs to inherit the base class SPJobDefinition and then it has to override the method Execute().
Here is the sample code we have to write for our business logic and then associate this class with feature receiver.
Regarding the feature scope, since this job runs under the central administration, we need to activate this feature at the web application level for which our custom list is getting updated
I worked on the small requirement to add the items for a custom list with predefined schedule.
Here we used the base class SPJobDefinition which is part of SharePoint, Administration namespace.
So we need to make sure to add the reference Microsoft.SharePoint.Administration reference and the namespace as well.
our custom timer job class needs to inherit the base class SPJobDefinition and then it has to override the method Execute().
Here is the sample code we have to write for our business logic and then associate this class with feature receiver.
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
class NotificationTimerJob:Microsoft.SharePoint.Administration.SPJobDefinition | |
{ | |
public NotificationTimerJob():base() | |
{ | |
} | |
public NotificationTimerJob(string jobName, SPService service) | |
: base(jobName, service, null, SPJobLockType.None) | |
{ | |
this.Title="Task list timer job"; | |
} | |
public NotificationTimerJob(string jobName, SPWebApplication webApp) | |
: base(jobName, webApp, null, SPJobLockType.None) | |
{ | |
this.Title = "Task list timer job"; | |
} | |
public override void Execute(Guid targetInstanceId) | |
{ | |
//base.Execute(targetInstanceId); | |
SPWebApplication webApplication1 = this.Parent as SPWebApplication; | |
SPList taskList = webApplication1.Sites[0].RootWeb.Lists["Tasks"]; | |
SPListItem item1 = taskList.Items.Add(); | |
item1["Title"] = "New Task" + DateTime.Now.ToShortDateString(); | |
item1.Update(); | |
} | |
} |
Regarding the feature scope, since this job runs under the central administration, we need to activate this feature at the web application level for which our custom list is getting updated
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
public class TaskListTimerEventReceiver : SPFeatureReceiver | |
{ | |
// Uncomment the method below to handle the event raised after a feature has been activated. | |
const string jobName = "Task list timer job"; | |
public override void FeatureActivated(SPFeatureReceiverProperties properties) | |
{ | |
SPSecurity.RunWithElevatedPrivileges(delegate() | |
{ | |
SPWebApplication webApp = properties.Feature.Parent as SPWebApplication; | |
DeleteExistingJob(jobName, webApp); | |
CreateJob(jobName, webApp); | |
}); | |
} | |
private void CreateJob(string jobName, SPWebApplication webApp) | |
{ | |
NotificationTimerJob timer1 = new NotificationTimerJob(jobName, webApp); | |
SPMinuteSchedule minuteschedule=new SPMinuteSchedule(); | |
minuteschedule.BeginSecond = 0; | |
minuteschedule.EndSecond = 59; | |
minuteschedule.Interval = 5; | |
timer1.Schedule = minuteschedule; | |
timer1.Update(); | |
} | |
private void DeleteExistingJob(string jobName, SPWebApplication webApp) | |
{ | |
foreach (SPJobDefinition job in webApp.JobDefinitions) | |
{ | |
if (job.Name == jobName) | |
{ | |
job.Delete(); | |
} | |
} | |
} | |
// 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 webApp = properties.Feature.Parent as SPWebApplication; | |
DeleteExistingJob(jobName, webApp); | |
} | |
); | |
} | |
catch | |
{ | |
} | |
} |
No comments:
Post a Comment