Pages

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: http://www.directsharepoint.com/2011/04/access-denied-while-activating-feature.html. 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:


SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    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;
                                job.RunNow();
                                break;
                            }
                        }
                    }
                });



2 comments:

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

    ReplyDelete