Tuesday, January 22, 2008

Sharepoint Timerjob: Read the web.config inside a Timer Job

There is no way to get a reference to a SPSite or SPWeb object inside a timerjob. So when you want to store configuration values in the web.config of your Web Application, there is no way to get the Web Application's name to open the web.config with the WebConfigurationManager. I programmed this workaround:

- In the FeatureReceiver of the Timer job, pass the SPSite instance of the site where the feature is activated on, to the constructor of your timer job class:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
// Collect the reference to the site from the feature parent that can be SPSite/SPWeb.
using (SPSite site = properties.Feature.Parent.GetType() == typeof(SPSite) ? (SPSite)properties.Feature.Parent : (SPSite)((SPWeb)properties.Feature.Parent).Site)
{

...

MailDateAlertsJob mailDateAlertsTimerJob = new MailDateAlertsJob(SanofiConstants.JOB_NAME, site.WebApplication, properties.Definition.DisplayName, site);

...

}

}

- In the constructur of your Timer job class, you will have a SPSite parameter:

public MailDateAlertsJob(string jobName, SPWebApplication webApp, string featureName, SPSite site)
: base(jobName, webApp, null, SPJobLockType.Job)
{
this.Title = SanofiConstants.JOB_TITLE;
this._siteToHandleActionsOn = site.Url;
}

- The _siteToHandleActionsOn variable holds the url string of the site where your feature is activated on. This variable is defined like this (note the [Persisted] attribute!!):

[Persisted]
private string _siteToHandleActionsOn;

- In the Execute methods of your timer job, you now can open the web configuration of the site:

if (this._siteToHandleActionsOn != null)
{

using (SPSite site = new SPSite(this._siteToHandleActionsOn))
{

...

string appSettingValue = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name).AppSettings.Settings["key"].Value;

}

}

}

If you have a better solution for this, please let me know ;-)

7 comments:

Unknown said...

Man,

This is the best solution, congratulations

JC Cristobal said...

cant we do something like this?
public override void Execute (Guid contentDbId) {
UpdateThumbnails(contentDbId, "Videos");
}

public void UpdateThumbnails(Guid contentDbId, String ListName)
{
// get a reference to the current site collection's content database
SPWebApplication webApplication = this.Parent as SPWebApplication;
SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];

using (SPSite siteCollection = new SPSite(contentDb.Sites[0].RootWeb.Url))
{
using (SPWeb curWeb = siteCollection.OpenWeb())
{

Configuration config = WebConfigurationManager.OpenWebConfiguration("/", siteCollection.WebApplication.Name);
string value = config.AppSettings.Settings["keyName"].value;

Mads Nissen said...

Could it be an option to use the PropertyBag on the JobDefinition instead?

mads
http://weblogs.asp.net/mnissen

Unknown said...

You can use this post to achieve your need. You can simply access complete web.config entries in the SharePoint timer job code.
http://praveenbattula.blogspot.com/2009/12/access-webconfig-in-sharepoint-timer.html

Anonymous said...

to get the central admin web and site use this:

Microsoft.SharePoint.Administration.SPAdministrationWebApplication caWebApp = Microsoft.SharePoint.Administration.SPAdministrationWebApplication.Local;
string url = caWebApp.Sites[0].Url;
SPSite mySite = new SPSite(url);
SPWeb myWeb = mySite.OpenWeb();

Anonymous said...

Cool post as for me. I'd like to read a bit more about this theme. Thnx for sharing this information. Luda
escorts in Kiev

e-sign act said...

Simply want to say your article is as astounding.
The clarity for your submit is just spectacular and that i could suppose you’re a professional