Sunday, December 27, 2009

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.

16 comments:

  1. Very interesting tutorial.

    It's work very well for me!!

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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.

    ReplyDelete
  4. Good information. Thanks!

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

    ReplyDelete
  6. 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

    ReplyDelete
  7. @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

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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.

    ReplyDelete
  10. I'm using Sharepoint 2007 and visual studio 2008.
    I'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!

    ReplyDelete
  11. I get the error " Configuration propert is inaccessible due its protection level"..Could you please help me on this.

    ReplyDelete
  12. Worked the first time. Much Appreciated.

    ReplyDelete
  13. Hello guy,
    I 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

    ReplyDelete