1

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?

1 Answer 1

2

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
            }
        }
    }
}
1
  • should ,the key = "mySiteUrl"; , is my site collection name/ hardcoded name?
    – samolpp2
    Commented Jun 30, 2017 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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