Home > SharePoint Products and Technology > Get SPweb, SPList or Document Library from SPContentDatabase Class

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}

}

}

}

Advertisements
  1. April 6, 2011 at 7:02 am

    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.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: