1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| using Microsoft.SharePoint;using Microsoft.SharePoint.Administration;namespace UpshotSP{ class ListTimerJob : SPJobDefinition { public ListTimerJob() : base() { } public ListTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType) : base(jobName, service, server, targetType) { } public ListTimerJob(string jobName, SPWebApplication webApplication) : base(jobName, webApplication, null, SPJobLockType.ContentDatabase) { this.Title = "List Timer Job"; } public override void Execute(Guid contentDbId) { // get a reference to the current site collection's content database SPWebApplication webApplication = this.Parent as SPWebApplication; SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId]; // get a reference to the "ListTimerJob" list in the RootWeb of the first site collection in the content database SPList Listjob = contentDb.Sites[0].RootWeb.Lists["ListTimerJob"]; // create a new list Item, set the Title to the current day/time, and update the item SPListItem newList = Listjob.Items.Add(); newList["Title"] = DateTime.Now.ToString(); newList.Update(); } }} |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| namespace UpshotSP.Features.Feature1{[Guid("9a724fdb-e423-4232-9626-0cffc53fb74b")]public class Feature1EventReceiver : SPFeatureReceiver { const string List_JOB_NAME = "ListLogger"; // Uncomment the method below to handle the event raised after a feature has been activated. public override void FeatureActivated(SPFeatureReceiverProperties properties) { SPSite site = properties.Feature.Parent as SPSite; // make sure the job isn't already registered foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == List_JOB_NAME) job.Delete(); } // install the job ListTimerJob listLoggerJob = new ListTimerJob(List_JOB_NAME, site.WebApplication); SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; schedule.EndSecond = 59; schedule.Interval = 5; listLoggerJob.Schedule = schedule; listLoggerJob.Update(); } // 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; // delete the job foreach (SPJobDefinition job in site.WebApplication.JobDefinitions) { if (job.Name == List_JOB_NAME) job.Delete(); } } }} |


