Accessing a connection string (stored in web Application web.config file) from within a Timer Job in Microsoft SharePoint 2013 or 2010. Follow this link for 2019.

Solution

Use WebConfigurationManager as detailed below

Explanation

Sometimes it is needed to access the web.config file within a Timer Job. In our case, we need to retrieve a connection string so that our Timer Job can connect to a database.

This can be achieved by the following:

Configuration webAppConfig = WebConfigurationManager.OpenWebConfiguration(“/”, this.WebApplication.Name);ConnectionStringSettings connString;if (webAppConfig.ConnectionStrings.ConnectionStrings.Count > 0){connString = webAppConfig.ConnectionStrings.ConnectionStrings[“OurConnectionString”];if (connString != null){ourConnectionString = connString.ConnectionString;}}

The following namespaces are required:

using System.Web.Configuration;using System.Configuration;

Ta-dah! From following these instructions you should now be able to access a connection string from within a Timer Job in Microsoft SharePoint 2013 and 2010.

 

Get Up To Speed With SharePoint 2019!

Download Our FREE eBook...

 

Download SharePoint 2019 eBook

Comments