Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timer Job from a SPWeb

I guess starting a timer job from within the code requires Farm admin credentials. However, I need to start a timer job from a web part that will be used in any site. Now when I try to start the job it gives me an access denied error obviously because the app pool identity is not farm admin. Any ideas on how to resolve this issue?

Thanks,

like image 912
Faiz Avatar asked May 05 '10 10:05

Faiz


2 Answers

Your are right. To start a timer job the app pool user has to be farm administrator. Since starting a timer job requires you to update a SPJobDefinition object with an SPSchedule. The SPJobDefinition is a SPPersistedObject which is stored in the SharePoint Config Database. Only farm administrators can write into this db.

I don't see a way to get pass this issue.

Workaround: Depending on your requirements you could write a master job that runs on regular basis. This job could query a SharePoint list and start another job defined by such a list item. Since the master job runs under the Farm Administrator account, the job would be able to start a new timer job.

like image 31
Stefan Avatar answered Sep 18 '22 12:09

Stefan


Timer jobs run as the farm administrator and are not intended to be triggered directly by an end-user. Since some jobs may be resource intensive, only the farm admin can create new jobs or modify the schedule for existing jobs.

One solution is to use the SPWorkItem infrastructure to queue user tasks which are then processed by a custom timer job derived from SPWorkItemJobDefinition. Your webpart would call SPSite.AddWorkItem to add the work item. When your timer job runs, it will look for any work items with the matching WorkItemType GUID and invoke the ProcessWorkItem overload.

like image 171
Joel Fillmore Avatar answered Sep 22 '22 12:09

Joel Fillmore