evoke it logo white with clear backQuick Contact

Accessing a Connection String within a Timer Job in SharePoint 2013 and 2010

Accessing a Connection String within a Timer Job in SharePoint 2013 and 2010

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

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.

Be the First to Get Our Ebook
Microsoft SharePoint 2019

What you need to know!

Back To The BlogContact Support

View Post's Via Catagory

IT support laptop image
Software Development cloud image
Terms & ConditionsPrivacy PolicyDeveloped by Evoke IT