Search This Blog

Thursday, March 22, 2012

Access denied on SPJobDefinition RunNow()

Recently I faced strange issue while executing a timer service through my web part. Since it was possible in MOSS 2007, but I know SharePoint 2010 does not support executing any timer job through code. I have already written a solution for setting up the property : RemoteAdministratorAccessDenied. This property allows modification of the SPPersistedObject from content web applications.  The post can be viewed at: However when I tried to execute the timer, It gave an error as:

"Operation is not valid due to the current state of the object"

To resolve this, I cleared the HttpContext.Current property to null as:

                    using (SPSite elevatedSite = new SPSite("Site URL"))
                        foreach (SPJobDefinition job in  elevatedSite.WebApplication.JobDefinitions)
                            if (job.Title.Equals("Your job name") && job.Status == SPObjectStatus.Online)
                                HttpContext.Current = null;


  1. Pankaj, The above code works without any 'access denied' issue. Let me know if you are facing issue with the code.