This is common scenario where some times we may need to access the web.config file in sharepoint custom timer job. But timer job is running under OWSTIMER.EXE, but sharepoint is running under the context of w3wp process, So its difficult to access the web.config file in the timer job code. To overcome this we need to create the instance of the webapp and Configuration and then we can access the web config file.
Below is the sample snippet code where we can access the web.config file.
//Get the instance of the current webapplication
SPWebApplication webApp = this.Parent as SPWebApplication;
//Create the webconfig instance by using web application name
Configuration webconfig = WebConfigurationManager.OpenWebConfiguration("/", webApp.Name);
//Get the configured value from web config using the key value in my case its "MYKEY"
string appValue = webconfig.AppSettings.Settings["MYKEY"].Value.ToString();
The above code needs to written in Execute method of the timer job definition file.
No comments:
Post a Comment