1

I am trying to run a timer job from a list using a ribbon button and on click of the button a page pops up. On that page load I have given runwithelevatedprivileges and job.runnow(). I am getting access denied exception in my production environment but it was working fine in system test. See the following code:

 SPWebApplication mainWebApp = SPContext.Current.Site.WebApplication;
 bool b = mainWebApp.RunningJobs.Cast<SPRunningJob>().Any(curRunningJob => curRunningJob
                    .JobDefinitionTitle.Equals("JobName"));
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        SPJobDefinition job = mainWebApp.JobDefinitions.Cast<SPJobDefinition>().
                            FirstOrDefault(i => i.DisplayName.Equals("JobName"));
SPContext.Current.Web.AllowUnsafeUpdates = true;
                                job.RunNow();//getting access denied exception at this line 
Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • First of all is not a good idea run a job from a page, write shared code instead. The problem I think is on user permission, connect to Job page with the user and try to run the job. – Max Mar 18 '16 at 08:45

1 Answers1

0

TimerJobs are Farm (configdb) items.
Therefore to do anything with them you need to be farm admin. Which means either the current user has to be farm admin (and then you might even have challenges depending on the authentication methods you're using).
Or the service account of the app pool runing the web app has to be farm admin and use runwithelevatedpriviledges which is a terrible idea for obvious security reasons.
Or you need to store some farm admin somewhere and create a context from that (which is also bad for security, performance and maintainability).
I'd strongly suggest you review your solution to avoid needing triggering timerjobs from the web interface.

baywet
  • 4,377
  • 4
  • 20
  • 49