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