{"id":3566,"date":"2012-06-12T12:56:36","date_gmt":"2012-06-12T12:56:36","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/building-a-list-of-all-sharepoint-timer-jobs-programmatically-in-c\/"},"modified":"2016-07-28T10:50:50","modified_gmt":"2016-07-28T10:50:50","slug":"building-a-list-of-all-sharepoint-timer-jobs-programmatically-in-c","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/building-a-list-of-all-sharepoint-timer-jobs-programmatically-in-c\/","title":{"rendered":"Building a List of All SharePoint Timer Jobs Programmatically in C#"},"content":{"rendered":"<p>One of the most frustrating things about SharePoint is that the difficulty in figuring something out is inversely proportional to the simplicity of what you are trying to accomplish.&#160; Case in point, yesterday I wanted to get a list of all the timer jobs in SharePoint.&#160; Having never done this nor having any idea of exactly how to do this right off the top of my head, I inquired to Google.&#160; I like to think my Google-fu is fair to good, so I normally find exactly what I&#8217;m looking for in the first hit.&#160; But on the topic of listing all SharePoint timer jobs all it came up with a PowerShell script command (Get-SPTimerJob) and nothing more. Refined search after refined search continued to turn up nothing.<\/p>\n<p>So apparently I am the only person on the planet who needs to get a list of the timer jobs in C#.&#160; In case you are the second person on the planet who needs to do this, the code to do so follows:<\/p>\n<p><code>SPSecurity.RunWithElevatedPrivileges(() =&gt;        <br \/>{         <br \/>&#160;&#160; var timerJobs = new List();         <br \/>&#160;&#160; foreach (var job in SPAdministrationWebApplication.Local.JobDefinitions)         <br \/>&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160; timerJobs.Add(job);         <br \/>&#160;&#160; }         <br \/>&#160;&#160; <\/code><code>foreach (SPService curService in SPFarm.Local.Services)        <br \/>&#160;&#160; <\/code><code>{        <br \/>&#160;&#160;&#160;&#160;&#160; foreach (var job in curService.JobDefinitions)         <br \/>&#160;&#160;&#160;&#160;&#160; {         <br \/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; timerJobs.Add(job);         <br \/>&#160;&#160;&#160;&#160;&#160; }&#160; <br \/>&#160;&#160; }         <br \/>});<\/code><\/p>\n<p>For reference, you have the two for loops because the Central Admin web application doesn&#8217;t end up being in the SPFarm.Local.Services group, so you have to get it manually from the SPAdministrationWebApplication.Local reference.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most frustrating things about SharePoint is that the difficulty in figuring something out is inversely proportional to the simplicity of what you are trying to accomplish.&#160; Case in point, yesterday I wanted to get a list of all the timer jobs in SharePoint.&#160; Having never done this nor having any idea of&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-3566","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=3566"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3566\/revisions"}],"predecessor-version":[{"id":42170,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3566\/revisions\/42170"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=3566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=3566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=3566"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=3566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}