简体   繁体   中英

Can't activate Job in sharepoint 2010

this code is an error in the line oJob.Update();

string JOB_NAME = "TestJob";
using (SPSite oSite = new SPSite("http://mysite", SPUserToken.SystemAccount))
{
NotifyJob oJob = new NotifyJob(JOB_NAME, oSite.WebApplication);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0;
schedule.EndSecond = 59;
schedule.Interval = 30;
oJob.Schedule = schedule;
oSite.AllowUnsafeUpdates = true;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
oJob.Update();
});
oSite.AllowUnsafeUpdates = false;
}



Error: System.Security.SecurityException: Access denied.
 at Microsoft.SharePoint.Administration.SPPersistedObject.BaseUpdate()
 at Microsoft.SharePoint.Administration.SPJobDefinition.Update()

First of all, please check if this solution applies to your scenario. From your code i'm guessing that your executing it from some kind of webpart/page.

http://unclepaul84.blogspot.com/2010/06/sppersistedobject-xxxxxxxxxxx-could-not.html

Second thing is that RunWithEvelatedPrivileges executes code under Application Pool identity. If you are executing it from normal SharePoint content site collection, which is running on different app pool (and account - which how it should be) then Central Administration, you still may not have access to Timer Job, which requires Farm Administrator rights.

don't obtain site or web from SPSite/SPWeb constuctor. you should take it from SPFeatureReceiverProperties

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    NotifyJob oJob = new NotifyJob("TestJob", web.Site.WebApplication);
    SPMinuteSchedule schedule = new SPMinuteSchedule();
    schedule.BeginSecond = 0;
    schedule.EndSecond = 59;
    schedule.Interval = 30;
    oJob.Schedule = schedule;
    oJob.Update();
}

or

SPSite site = properties.Feature.Parent as SPSite;

if your feature scope is Site

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM