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.
Very interesting tutorial.
ReplyDeleteIt's work very well for me!!
Hey,
ReplyDeleteNeed 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
If you want to make a web request then it's very easy, you can use below code to get that.
ReplyDeleteSystem.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.
Good information. Thanks!
ReplyDeleteHow do we do this for an extended web application? If I use webApplication.Name it grabs the non-extended webconfig
ReplyDeleteCool!!!
ReplyDeleteGreat!! Thanks
ReplyDeleteHi there,
ReplyDeleteI 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
@Khushi
ReplyDeleteanswering 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
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.
ReplyDeleteAfter reading your article it makes perfect sense. Thank you for taking the time to post this!!
Much appreciated.
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.
ReplyDeleteAfter reading your article it makes perfect sense. Thank you for taking the time to post this!!
Much appreciated.
Thanks a lot ;)
ReplyDeleteI'm using Sharepoint 2007 and visual studio 2008.
ReplyDeleteI'm able to deploy, activate a feature corresponding to a timer job but unable to view it under Timer job definitions in Central Admin. Please suggest.
Thanks in advance!
I get the error " Configuration propert is inaccessible due its protection level"..Could you please help me on this.
ReplyDeleteWorked the first time. Much Appreciated.
ReplyDeleteHello guy,
ReplyDeleteI have praticed as your instruction but the web application instance is null for my timer job.
Anyone can tell me how to fix this error, please. Thanks
Thanks a lot for Sharing this.
ReplyDeleteReally helped me a lot.