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
}
}
Related Post
OWSTimer.exe
SharePoint 2010
- List view render using JSLink with Fabric UI
- Delete Quick Links using Powershell
- delete/Add Top Navigation Links using Powershell
- PowerShell::Get Content Database Size in SharePoint
- PowerShell :: Delete All List Items
- Basics: Difference between Synchronous Events and Asynchronous Events in SharePoint
- Remove-SPSite 0x80070003 - Forcely delete Site Collection that cannot be deleted
- This operation can be performed only on a computer that is joined to a server farm by users who have permissions in SQL Server to read from the configuration database. To connect this server to the server farm, use the SharePoint Products Configuration Wizard, located on the Start menu in Microsoft SharePoint 2010 Products.
- Restore-SPSite: The operation that you are attempting to perform cannot be completed successfully.
- Get List Field Type using ECMA Script in SP Hosted Apps
- SharePoint Interview Questions and Answers..
- What are Event Receivers and How to Create Sample event Receivers and attach to List/ Doc Library ?
- Export SharePoint Search Crawl log to CSV File.
- Create Content Type using ECMA Script
- Get List Content Types using ECMA Script
- SharePoint Modal Dailoge to Open Page
- SPDataSource in SharePoint.
- Capacity planning and Sizing in SharePoint 2010
- Enable 'Save site as Template' in Publishing Sites by using SharePoint Designer
- Read Enhanced Rich Text Values in SharePoint
- What is Web part life Cycle in SharePoint 2013 and SharePoint 2010
- How to Hide Controls in SharePoint New Form Based on User using ECMA Script.
- What is the Content Type for Custom List in SharePoint 2013, SharePoint 2010
- What is calculated column in SharePoint 2013 ,2010
- Create Meetings by using Exchange Server API
0 comments:
Post a Comment