Timer job in SharePoint for specific site
In this post we will see how to create Timer job in SharePoint 2007 / SharePoint 2010.What are the points that are covered
- Creating custom Timer job
- How to execute Timer job for a specific site (without changes to web config and without any static siteurl)
- How to pass values to property bag of Timer job from feature
SharePoint Timer Job
The following code creates Timerjob class that inherits from SPJobDefinition
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
| using System; using System.Collections.Generic; using System.Web; using Microsoft.SharePoint.Administration; using Microsoft.SharePoint; namespace TestSharepointProject { public class TimerJob : SPJobDefinition { SPWeb mySiteWeb; string mySiteUrl = "" ; public TimerJob(SPWebApplication webApp): base ( "TimerJob" , webApp, null , SPJobLockType.ContentDatabase) { this .Title = "TimerJob" ; } public TimerJob(): base () { } public override void Execute(Guid targetInstanceId) { if (! string .IsNullOrEmpty( this .Properties[ "mySiteUrl" ].ToString())) { mySiteUrl = this .Properties[ "mySiteUrl" ].ToString(); } if (! string .IsNullOrEmpty(mySiteUrl)) { using (SPSite mySite = new SPSite(mySiteUrl)) { using (mySiteWeb = mySite.OpenWeb()) { //provide your logic here for the site } } } } } } |
Now our timer job class is ready and we will see how we can add key value pairs in the property bag of our custom timer job class.
Create a feauture “TimerJobFeatureReceiver” with scope as web. Following is the code for the feauture reciever which adds key values.
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
64
65
66
67
68
69
70
71
72
| using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; namespace TestSharepointProject { public class TimerJobFeatureReceiver : SPFeatureReceiver { public override void FeatureActivated(SPFeatureReceiverProperties properties) { try { SPSecurity.RunWithElevatedPrivileges( delegate () { SPWeb web = properties.Feature.Parent as SPWeb; web.AllowUnsafeUpdates = true ; SPWebApplication webApp = web.Site.WebApplication; foreach (SPJobDefinition job in webApp.JobDefinitions) if (job.Name == "TimerJob" ) job.Delete(); string key = "mySiteUrl" ; string value = web.Url; TimerJob tmrJob = new TimerJob(webApp); //remove the key if already exists bool isKeyExists = tmrJob.Properties.ContainsKey(key); if (isKeyExists) { tmrJob.Properties.Remove(key); } tmrJob.Properties.Add(key, value); SPMinuteSchedule schedule = new SPMinuteSchedule(); schedule.BeginSecond = 0; //to start immediately // schedule.EndSecond = 59; //use this if timer job is to end after some seconde schedule.Interval = 60; //number of minutes tmrJob.Schedule = schedule; tmrJob.Update(); web.AllowUnsafeUpdates = false ; }); } catch (Exception ex) { //log exception if any } } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { try { //remove the scheduled job SPSecurity.RunWithElevatedPrivileges( delegate () { SPWeb web = properties.Feature.Parent as SPWeb; web.AllowUnsafeUpdates = true ; SPWebApplication webApp = web.Site.WebApplication; foreach (SPJobDefinition job in webApp.JobDefinitions) if (job.Name == "TimerJob" ) job.Delete(); web.AllowUnsafeUpdates = false ; }); } catch (Exception ex) { //log exception if any } } } } |
Read the custom list in feature acitvation event and add those values in the timerjob property bag. In this way we can maintain config values to
timer job without any changes to web.config just to read some key values.
Remarks
Make sure that the user with whom we are activating the feature should have valid permission as the code is dealing with timer job.We may encounters some errors like
Access denied.
at Microsoft.SharePoint.Administration.SPPersistedObject.Update()
at Microsoft.SharePoint.Administration.SPJobDefinition.Update()
at Pages.SurveyFeatureReceiver.<>c__DisplayClass1.
Its suggestable to activate the feature from command prompt and we won’t be getting the above though both are running with same user.
Following is the quick code to activate or deactivate the feature by stsadm
Activating the feature
stsadm -o activatefeature -fileName TimerJobFeatureReceiver\Feature.xml -url http://adicodes/sites/mysite -force
Deactivating the feature
stsadm -o deactivatefeature -fileName TimerJobFeatureReceiver
No comments:
Post a Comment