Timer Lavoro Distribuzione con privilegi elevati
-
10-12-2019 - |
Domanda
Ho scritto un lavoro timer CUSOM in cui si attiva la funzionalità che sto usando il codice seguente. Funziona in devlopment ma quando l'ho distribuito in QA ottengo il seguente errore. L'utente è un amministratore di fattoria. Qualche cosa sbaglia nel mio codice?
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite objsite = SPContext.Current.Site;
SPWeb objweb = SPContext.Current.Web;
SPSite ElevatedSite = new SPSite(objsite.Url);
SPWeb ElevatedWeb = ElevatedSite.OpenWeb(objweb.Url);
objweb.AllowUnsafeUpdates = true;
SPWebApplication webApp = objweb.Site.WebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
if (job.Name == "TaskAlertTimerJob") job.Delete();
string key = "mySiteUrl";
string value = objweb.Url;
TaskAlert tmrJob = new TaskAlert(webApp);
bool isKeyExists = tmrJob.Properties.ContainsKey(key);
if (isKeyExists)
{
tmrJob.Properties.Remove(key);
}
tmrJob.Properties.Add(key, value);
SPMinuteSchedule schedule = new SPMinuteSchedule();
schedule.BeginSecond = 0; //to start immediately
schedule.EndSecond = 59; //use this if timer job is to end after some seconde
schedule.Interval = 1; //number of minutes
tmrJob.Schedule = schedule;
tmrJob.Update();
objweb.AllowUnsafeUpdates = false;
});
}
catch (Exception ex)
{
//log exception if any
}
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
//remove the scheduled job
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite objsite = SPContext.Current.Site;
SPWeb objweb = SPContext.Current.Web;
SPSite ElevatedSite = new SPSite(objsite.Url);
SPWeb ElevatedWeb = ElevatedSite.OpenWeb(objweb.Url);
objweb.AllowUnsafeUpdates = true;
SPWebApplication webApp = objweb.Site.WebApplication;
foreach (SPJobDefinition job in webApp.JobDefinitions)
if (job.Name == "TaskAlertTimerJob") job.Delete();
objweb.AllowUnsafeUpdates = false;
});
}
catch (Exception ex)
{
//log exception if any
}
}
.
Nel metodo di esecuzione ho scritto come questo
public override void Execute(Guid targetInstanceId)
{
if (!string.IsNullOrEmpty(this.Properties["mySiteUrl"].ToString()))
{
mySiteUrl = this.Properties["mySiteUrl"].ToString();
}
if (!string.IsNullOrEmpty(mySiteUrl))
{
using (SPSite mySite = new SPSite(mySiteUrl))
{
using (mySiteWeb = mySite.OpenWeb())
{
DateTime dtNow = DateTime.Now;
SPList Sourcelist = mySiteWeb.Lists.TryGetList("TaskList");
SPQuery mySourceListQuery = new SPQuery();
mySourceListQuery.Query = "Query"
SPListItemCollection mySourceItemColl = Sourcelist.GetItems(mySourceListQuery);
if (mySourceItemColl.Count > 0)
{
int count = 0;
foreach (SPListItem mySourceListItem in mySourceItemColl)
{
// My Code Goes Here
count++;
}//End of for loop
} //End of if loop
}
}
}
}
.
Il mio costruttore e il mio metodo è come questo
public TaskAlert(SPWebApplication webApplication):base("TaskAlertTimerJob", webApplication, null, SPJobLockType.None)
{
Title = "TaskAlertTimerJob";
}
.
Errore: Il nome SPPERSISTisteDobject, TaskAlent= TaskAlertTtimerJob, non è stato possibile aggiornare perché l'utente corrente non è un amministratore di fattoria. 45DB89E8-DBEF-4C1C-8405-46ACB8AB6E82 18/06/2013 06: 34: 57.33 W3WP.EXE (0x2108) 0x1F0C SharePoint Foundation Generale 75F8 Attivazione delle caratteristiche medie: Caratteristica 'TaskAlertSchedular_TaskAlentFeature' (ID: 'B434BB08-CAEA-44C3-B8D6-CC3A32F3507C') È stato attivato all'URL http://santest.com/sites/mysite . 45DB89E8-DBEF-4C1C-8405-46ACB8AB6E82
Soluzione
Simple,
Your using runwithelevated privlages means your account that your running under is application pool not the current logged in user!
Instead you need to also use impersonation with elevated priv to get the wanted result!
check my answer here in detail! :)
Altri suggerimenti
Deploy your feature on SharePoint Central Administration web application.