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 ;-)

4 comments:

Norman Cano (Barca Solutions) 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;

ghkj said...

However mean your gold in wow life is,buy wow gold meet it and live it ;wow gold cheap do not shun it and call it hard names.It is not so bad as you are.It looks poorest when you are richest.The fault-finder will find faults in paradise.wow gold kaufen love your life,poor as it is.maple meso You may perhaps have some pleasant,thrilling,maplestory power leveling glorious hourss,even in a poor-house.The setting sun is reflected from the windows of the alms-house as brightly as from the rich man's abode;the snow melts before its door as early in the spring.sell wow gold I do not see but a quiet mind may live as contentedly there,cheap mesos and have as cheering thoughts,as in a palace.The town's poor seem to me often to live the most independent lives of any.May be they are simply great enough to billig wow gold receive without misgiving.Most think that they are above being supported by the town;powerlevel but it often happens that buy maplestory mesos they are not above supporting themselves by dishonest means.which should be more disreputable.Cultivate poverty like a garden herb,like sage.Do not trouble wow powerleveln yourself much to get new things,maple mesos whether clothes or friends

Mads Nissen said...

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

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