{"id":2901,"date":"2011-12-01T10:34:43","date_gmt":"2011-12-01T02:34:43","guid":{"rendered":"http:\/\/www.sharepointboost.com\/blog\/?p=2901"},"modified":"2023-07-31T11:40:23","modified_gmt":"2023-07-31T03:40:23","slug":"developing-customtimerjobs","status":"publish","type":"post","link":"https:\/\/www.boostsolutions.com\/blog\/developing-customtimerjobs\/","title":{"rendered":"Developing CustomTimerJobs"},"content":{"rendered":"<p>A custom timer job within SharePoint enables you to add a scheduled task to a SharePoint application. This blog post describes how to create and deploy Custom Timer Jobs.<strong> <\/strong><\/p>\n<h2><strong>Creating Custom Timer Jobs<\/strong><\/h2>\n<p>Create a Custom Timer Job in SharePoint is very simple, you can declare a class which inherits from <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.administration.spjobdefinition.aspx\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color: #3366ff\">SPJobDefinition<\/span><\/a> and overrides the Execute method as shown in the following example :<\/p>\n<pre lang=\"C#\">    \r\n    public class SampleTimerJob : SPJobDefinition\r\n    {\r\n        public const string JOBNAME = \"SAMPLETIMERJOB\";\r\n        public const string JOBTITLE = \"Sample Timer Job\";\r\n\r\n        public SampleTimerJob()\r\n        {\r\n            this.Title = JOBTITLE;\r\n        }\r\n\r\n        public SampleTimerJob(SPWebApplication webApplication)\r\n            : base(JOBNAME, webApplication, null, SPJobLockType.Job)\r\n        {\r\n            Title = JOBTITLE;\r\n        }\r\n\r\n        public override void Execute(Guid targetInstanceId)\r\n        {\r\n            \/\/ write your code here\r\n        }\r\n    }<\/pre>\n<p><!--more-->Noteworthy is the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.administration.spjobdefinition.aspx\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color: #3366ff\">SPJobDefinition<\/span><\/a> constructor(s) which accepts a SPJobLockType value. This parameter controls the locking behavior of the timer job and can be one of the following values:<\/p>\n<p><strong>ContentDatabase<\/strong>-Locks the content database. A timer job runs one time for each content database associated with the Web application.<strong> <\/strong><\/p>\n<p><strong>Job<\/strong>-Locks the timer job so that it runs only on one machine in the farm.<br \/>\n<strong> <\/strong><\/p>\n<p><strong>None<\/strong>-Provides no locks. The timer job runs on every machine in the farm on which the parent service is provisioned, unless the job is associated with a specified server. In this case, it runs on that server only (and only if the parent service is provisioned on the server).<\/p>\n<p>Based on these options, you must decide which value is correct for your tasks, the SPJobLockType.Job option is most likely the option to use.<\/p>\n<p><strong>DeployingCustom Timer Jobs<\/strong><\/p>\n<p>After you have created a timer job, the next step is to deploy it. Unfortunately, there is no provision in XML to deploy timer jobs. You can create custom applications, use PowerShell, or create a custom STSADM command to deploy the custom timer job. However, another technique is to use a web application-scoped feature to deploy the custom timer job. In your feature receiver, register the custom timer job as shown in the following example:<\/p>\n<pre lang=\"c#\">    public class SampleFeatureReceiver : SPFeatureReceiver\r\n    {\r\n        public override void FeatureActivated(SPFeatureReceiverProperties properties)\r\n        {\r\n            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;\r\n            if (webApp == null)\r\n            {\r\n                throw new ArgumentNullException(\"webApp\");\r\n            }\r\n\r\n            foreach (SPJobDefinition job in webApp.JobDefinitions)\r\n            {\r\n                if (job.Name == SampleTimerJob.JOBNAME)\r\n                {\r\n                    job.Delete();\r\n                    break;\r\n                }\r\n            }\r\n\r\n            SPHourlySchedule schedule = new SPHourlySchedule();\r\n            schedule.BeginMinute = 0;\r\n            schedule.EndMinute = 59;\r\n\r\n            SampleTimerJob sampleJob = new SampleTimerJob(webApp);\r\n            sampleJob.Schedule = schedule;\r\n            sampleJob.Update();\r\n        }\r\n\r\n        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)\r\n        {\r\n            SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;\r\n            if (webApp == null)\r\n            {\r\n                throw new ArgumentNullException(\"webApp\");\r\n            }\r\n\r\n            foreach (SPJobDefinition job in webApp.JobDefinitions)\r\n            {\r\n                if (job.Name == SampleTimerJob.JOBNAME)\r\n                {\r\n                    job.Delete();\r\n                    break;\r\n                }\r\n            }\r\n        }\r\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A custom timer job within SharePoint enables you to add a scheduled task to a SharePoint application. This blog post describes how to create and deploy Custom Timer Jobs. Creating Custom Timer Jobs Create a Custom Timer Job in SharePoint is very simple, you can declare a class which inherits from SPJobDefinition and overrides the [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[125],"tags":[272,271],"_links":{"self":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2901"}],"collection":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2901"}],"version-history":[{"count":18,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2901\/revisions"}],"predecessor-version":[{"id":9272,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2901\/revisions\/9272"}],"wp:attachment":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}