Sunday, December 27, 2009

Access web.config in SharePoint timer job

In SharePoint customization we have the requirements to write custom code for web parts, timer jobs etc. In this post I want to tell you some interesting things in SharePoint. I have written some timer jobs to deploy into my SharePoint server to match some requirements or to solve some problems.

My requirement is, I want to access the web application or web site configuration [web.config] file in the SharePoint timer job. But, the limitations in SharePoint stops me to access the configuration files in timer job. Below are the problems I faced.

  • SharePoint timer job is running in different process named OWSTIMER.EXE.
  • SharePoint web application or site will run with the help of process W3WP.EXE.
  • So, the application configuration file is associated with the W3WP.EXE, so there is no way to access the web.config file in the owstimer.exe process at all. The context is completely different and out of domain. So, we need to call or access the web.config file explicitly in timer job. How to?
  • Remember, always C# is completely object oriented and you can access everything through objects.
  • Usually the timer job installed as a feature. And all the logic of the timer job will go inside the Execute() method. So, write below statements in that method and access the complete web.config file. Below is the example of accessing appsettings.
SPWebApplication webApplication = this.Parent as SPWebApplication;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
String appValue = config.AppSettings.Settings["appSettingKey"].Value;

WebConfigurationManager is the class which is in the namespace Systen.Web.Configuration. This way, you can access the config settings from a web.config of a SharePoint web site. Hope you understood this well and please let me know what you think.

11 comments:

Anonymous said...

Very interesting tutorial.

It's work very well for me!!

Anonymous said...

Hey,

Need some help. I want to create a Timer Job in sharepoint 2007 which will just hit a url.

Ex: http://abc.com?do=process

I want the above url to be hit every time the job executes. How to do?

Thanks in advance.

Sumeet

Praveen said...

If you want to make a web request then it's very easy, you can use below code to get that.

System.Net.WebRequest req = System.Net.WebRequest.Create(requestUriString);
System.Net.WebResponse result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader(ReceiveStream, encode);
string ResponseCode = sr.ReadToEnd();
//Console.WriteLine(ResponseCode);
sr.Close();

If the url is a web service then you can directly add the reference and call it from c#. Let me know, if you need any more help.

Anonymous said...

Good information. Thanks!

Frodo82 said...

How do we do this for an extended web application? If I use webApplication.Name it grabs the non-extended webconfig

André Pedroso said...

Cool!!!

Littleyonky said...

Great!! Thanks

Khushi said...

Hi there,
I can see values in watch window of WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);

but config object is coming null.

Also, like to mention that config object is of System.Consifuration.Configuration type and WebConfiguration lies in System.Web.Configuration. Is it causing the problem???


second problem is I have custom section in web.config file. I am not able to read them from Timer Job. Any ideA???
Thanks
Khushi

André Pedroso said...

@Khushi

answering your 1st question... Yes! You have to use webconfiguration rather configuration

2nd question... After you load correctly web.config, you should be able to read your custom config sections.


Cheers

Jinx said...

Man, I worked on this for 2 days....not realizing I couldn't access the web.config of my web app from a timer job.

After reading your article it makes perfect sense. Thank you for taking the time to post this!!

Much appreciated.

Jinx said...

Man, I worked on this for 2 days....not realizing I couldn't access the web.config of my web app from a timer job.

After reading your article it makes perfect sense. Thank you for taking the time to post this!!

Much appreciated.