Get site collection in timer job
-
04-10-2020 - |
Domanda
I use this code to get the site collection :
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
SPSite Site = contentDb.Sites["sites/AA"];
SPWeb Web = rootSite.OpenWeb("BB");
the fact is Site == null
because contentDb.Site
contains only one site collection(the root site)
How can I get the siteCollection named: sites/AA
?
Soluzione
Use the timerjob properties collection and retrieve it in the Execute method of timerjob
follow below url contains solution: http://adicodes.com/timer-job-in-sharepoint-for-specific-site/
Code in the feature activated event store the siteUrl
string key = "mySiteUrl";
string value = web.Url;
TimerJob tmrJob = new TimerJob(webApp);
//remove the key if already exists
bool isKeyExists = tmrJob.Properties.ContainsKey(key);
if (isKeyExists)
{
tmrJob.Properties.Remove(key);
}
tmrJob.Properties.Add(key, value);
Get the url in the Execute method
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())
{
//provide your logic here for the site
}
}
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange