Hi All,
In a below post , i would like to explain how we can access Share Point Web Application Configuration (web.config) file from Custom Timer Job .
In General scenario accessing the Web.config values is straight forward as it is running under W3WP.exe But as SharePoint Timer Job runs in OWSTimer.exe Context , we cannot access directly.
As the context of both are different so we have to use WebConfigurationManager to get the specific web application configuration .
Generally TimerJob logic goes inside Execute() method , we are going to access web.config values here.
Execute() Method:
using System.Web.Configuration;
using System.Configuration;
public override void Execute(Guid targetInstanceId)
{
try
{
SPWebApplication webapplication = this.Parent as SPWebApplication;
string siteurl = webapplication.Sites[1].Url;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webapplication.Name);
if (!string.IsNullOrEmpty(siteurl))
{
Method(siteurl, config);
}
}
catch (Exception ex)
{
//Exception
}
}
public void Method(string siteUrl, Configuration config)
{
try
{
using (SPSite oSite = new SPSite(siteUrl))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
string fromEmailId = config.AppSettings.Settings["XXX"].Value;
}
}
}
catch (Exception ex)
{
//Exception
}
}
In a below post , i would like to explain how we can access Share Point Web Application Configuration (web.config) file from Custom Timer Job .
In General scenario accessing the Web.config values is straight forward as it is running under W3WP.exe But as SharePoint Timer Job runs in OWSTimer.exe Context , we cannot access directly.
As the context of both are different so we have to use WebConfigurationManager to get the specific web application configuration .
Generally TimerJob logic goes inside Execute() method , we are going to access web.config values here.
Execute() Method:
using System.Web.Configuration;
using System.Configuration;
public override void Execute(Guid targetInstanceId)
{
try
{
SPWebApplication webapplication = this.Parent as SPWebApplication;
string siteurl = webapplication.Sites[1].Url;
Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webapplication.Name);
if (!string.IsNullOrEmpty(siteurl))
{
Method(siteurl, config);
}
}
catch (Exception ex)
{
//Exception
}
}
public void Method(string siteUrl, Configuration config)
{
try
{
using (SPSite oSite = new SPSite(siteUrl))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
string fromEmailId = config.AppSettings.Settings["XXX"].Value;
}
}
}
catch (Exception ex)
{
//Exception
}
}
0 comments:
Post a Comment