Get SPweb, SPList or Document Library from SPContentDatabase Class
Hi,
This post is helpful for those who are trying to extract information from the SPContentDatabase class. This is usually required when one is creating custom timer job, base level features etc.
The following code is written for Custom timer job:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration; (must)
using Microsoft.SharePoint;
namespace TTKTimerJob
{
class TTKIssueTimerJob: SPJobDefinition
{
public TTKIssueTimerJob()
: base()
{ }
public TTKIssueTimerJob(string jobName, SPService service, SPServer server, SPJobLockType targetType)
: base(jobName, service, server, targetType)
{ }
public TTKIssueTimerJob(string jobName, SPWebApplication webApplication)
: base(jobName, webApplication, null, SPJobLockType.ContentDatabase)
{
this.Title = “TTK Issue Timer Job”;
}
public override void Execute(Guid targetInstanceId)
{
// get a reference to the current site collection’s content database
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[targetInstanceId];
// Get the TimerJobList Name from the webApplication collection
SPSiteCollection timerSiteCollection = webApplication.ContentDatabases[targetInstanceId].Sites;
SPList timerJobList = null;
foreach (SPSite siteItem in timerSiteCollection)
{
#region code here: finds the List in rootweb
//assumes the existence of a site collection with an absolute URL of
//http://localhost/sites/sitecollection/subsite/Lists/abc and that this site collection has a Web site named subsite
//SPList xyz = siteItem.RootWeb.GetList(“list url here”);
//SPList xyz = siteItem.RootWeb.Lists.TryGetList(“list Title here”);
timerJobList = siteItem.RootWeb.Lists.TryGetList(“ListTimerJob”);
if (timerJobList != null)
{
// create a new list Item, set the Title to the current day/time, and update the item
SPListItem newList = timerJobList.Items.Add();
newList["Title"] = DateTime.Now.ToString();
newList.Update();
break;
}
#endregion}
}
}
}

Great post. Just a heads up – I am running Ubuntu with the beta of Firefox and the navigation of your blog is kind of broken for me.