Get the rootweb in a SharePoint timerjob

I developed a timerjob for SharePoint and need to get the rootweb. And by looking a bit around i came around this great solution. It’s pretty simple, just follow the code below to get the rootweb

public override void Execute (Guid contentDbId)
{
    // get the current site collections content database
    SPWebApplication webApplication = this.Parent as SPWebApplication;
    SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

    // get the root web
    SPWeb rootweb = contentDb.Sites[0].RootWeb;

    //Do some work with the root web.
    //...
}

Then you have the rootweb of the site collection that the timerjob is owned by.

Tags:

About Brian Haahr-Nielsen

Certified SharePoint developer and soon to become Windows Mobile Manga developer