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 ;-)
Posted by Knrs at 19:04 http://www.blogger.com/post-edit.g?blogID=4727666002079866855&postID=3522721408211545383&from=pencilhttp://www.blogger.com/post-edit.g?blogID=4727666002079866855&postID=3522721408211545383&from=pencil
7 comments:
http://www.blogger.com/profile/05218781454398057329http://www.blogger.com/profile/05218781454398057329Norman Cano (Barca Solutions) said...
Man,
This is the best solution, congratulations
May 21, 2008 5:09 PM http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=5887508818941917486http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=5887508818941917486
http://www.blogger.com/profile/13081729731551523944http://www.blogger.com/profile/13081729731551523944JC 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;
October 5, 2008 11:01 AM http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=472322928915722090http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=472322928915722090
http://www.blogger.com/profile/09132300810330379086http://www.blogger.com/profile/09132300810330379086ghkj 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
February 3, 2009 10:15 AM http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=8186007594296434236http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=8186007594296434236
http://www.puzzlepart.com/http://www.puzzlepart.com/Mads Nissen said...
Could
it be an option to use the PropertyBag on the JobDefinition instead?
mads
http://weblogs.asp.net/mnissen
April 2, 2009 8:42 PM http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=6704500141333089202http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=6704500141333089202
http://www.blogger.com/profile/11277148819153438060http://www.blogger.com/profile/11277148819153438060Praveen 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
April 6, 2010 5:52 AM http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=4301680161299647231http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=4301680161299647231
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();
November 17, 2010 1:17 PM http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=3853547350614979838http://www.blogger.com/delete-comment.g?blogID=4727666002079866855&postID=3853547350614979838
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