Reading from web.config’s AppSettings in a SharePoint timer job

Posted: 8th April 2011 in Configuration, SharePoint, Sharepoint 2010

Well, this was not as straight-forward as i first thought. Why? Because the timer job does not run in the same App Domain as the web application. They are in two separate processes : W3WP.EXE  and OWSTIMER.EXE for the Web Application worker process and the SharePoint timer respectively.

So, how do you go about it?

Well, firstly you need to add reference to your timer job as follows (unless your using VB !):

using System.Configuration;
using System.Web.Configuration;

Reading AppSettings in Timer
  1. using (SPSite site = new SPSite("http://SiteCollectionUrl"))
  2.         {
  3.             Configuration config = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name);
  4.             return config.AppSettings.Settings["AppSettingKeyToRead"].Value;
  5.         }

Admittedly, this solution is hard coding the Uri of the Site Collection, but this is purely for demonstration purposes. This can be overcome by writing to a property as described in my previous posting Custom SharePoint Job Properties

Happy coding Winking smile

Advertisements
Comments
  1. […] Duthie, he mentioned he had done something similar and blogged about it. After reading his blog, Reading from web.config’s AppSettings in a SharePoint timer job, I thought this would be nice and easy. The complication arose from the fact the client had a web […]

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