SharePoint timerjob: Read the Web. config inside a timer job

Source: Internet
Author: User

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
[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). Deleetask. 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 = penpencil

 

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,200 8 pm http://www.blogger.com/delete-comment.g? Blogid = 4727666002079866855 & postid = 5887508818941917486 http://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 am http://www.blogger.com/delete-comment.g? Blogid = 4727666002079866855 & postid = 472322928915722090 http://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 glorous hourss, even in
A poor-house.The setting sun is reflected from the windows of the alms-house
Brightly as from the rich man's abode; the snow melts before its door as early
In the spring. WOW Gold I do not see but a quiet mind may
Live as contentedly there, cheap mesos and have as cheering thoughts, as in
Palace. The town's poor seem to me often to live the most independent lives
Any. May be they are simply great enough to billig wow
Gold receive without misgiving. Most think that they are abve being
Supported by the town; powerlevel but it often happens that buy
Maplestory mesos they are not abve supporting themselves
Dishonest means. Which shocould be more disreputable. Cultivate poverty like
Garden herb, like sage. Do not trouble wow
Powerleveln yourself much to get new things, maple mesos
Whether clothes or friends

February 3, 2009 am http://www.blogger.com/delete-comment.g? Blogid = 4727666002079866855 & postid = 8186007594296434236 http://www.blogger.com/delete-comment.g? Blogid = 4727666002079866855 & postid = 8186007594296434236

Http://www.puzzlepart.com/http://www.puzzlepart.com/Mads nissen.
Said...

Cocould
It be an option to use the propertybag on the jobdefinition instead?

Mads
Http://weblogs.asp.net/mnissen

Megal 2, 2009 PM http://www.blogger.com/delete-comment.g? Blogid = 4727666002079866855 & postid = 6704500141333089202 http://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

Megal 6, 2010 am http://www.blogger.com/delete-comment.g? Blogid = 4727666002079866855 & postid = 4301680161299647231 http://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,201 0 pm http://www.blogger.com/delete-comment.g? Blogid = 4727666002079866855 & postid = 3853547350614979838 http://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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.