Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

When i tried to Activate Feature of Timer Job It returns error : Access Denied

Scope of Feature is "Site"..

private static void CreateJob(SPSite site)       
 {          
  try        
    {
     SPSecurity.RunWithElevatedPrivileges(delegate()
     {               
     TenderTimerJob job = new TenderTimerJob(JobName, site.WebApplication);
     job.Properties.Add("SiteCollectionUrl", site.Url);
     SPMinuteSchedule minutes = new SPMinuteSchedule();
     minutes.BeginSecond = 0;
     minutes.Interval = 30;
     minutes.EndSecond = 30;
     job.Schedule = minutes;
     job.Update(); //Error Generates here
     });
    }
    catch (Exception ex){
     string str = ex.Message.ToString();
   }
}
share|improve this question
add comment

3 Answers 3

up vote 2 down vote accepted

When a timer job instance is created, it is persisted to the farm configuration database. Accessing this database for write purposes is a privileged operation; as a rule of thumb, only the farm service account (that is, the account under which OWSTIMER.EXE executes) or accounts that explicitly have the rights. So application pool account wouldn't work, as this this task requires farm level permissions. However, if you're developing in Visual Studio 2010 you can avoid automatic activation of features using Project-> Properties -> In the Active Deployment Configuration section select "No Activation"

share|improve this answer
    
Thank You Falak, –  Torque Oct 17 '12 at 7:58
    
I also tried without SpSecurity and my account is registered as Farm service account.. But Still not Working..:( –  Torque Oct 17 '12 at 8:05
    
Are you activating features using PowerShell or during development Visual Studio 2010? –  Falak Mahmood Oct 17 '12 at 10:54
    
During Deployment from VS 2010.. right now it is not showing error while i deploy it.. but when i debug it and activate feature manually it is showing error "Access Denied".. –  Torque Oct 17 '12 at 11:04
add comment

One thing to remember when you are using site-scoped or site collection–scoped Features to install the timer jobs: The account that does the scheduling must have write permissions to the configuration database of the server farm. Usually, the identity of the application pool that is hosting the SharePoint site does not have permissions on the configuration database.

Add the below function in the .cs file of timer job in which you can also find "Execute" function.

protected override bool HasAdditionalUpdateAccess()
  {
       return true;
  }

This will solve you "Access Denied" issue .


share|improve this answer
add comment

I ended up on this page as a result of a google search, but found this answer as well which helped me, summed up it says:

By default, attempting to instantiate a timer job from within the site collection context is going to fail. Attempting the operation in an elevated privilege block (via SPSecurity.RunWithElevatedPrivileges) is only going to result in the web application's application pool account context being used instead of the current user context; this only succeeds if the app pool account has rights to write to the farm configuration database.

for the full answer check http://stackoverflow.com/a/1038527/119387.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.