Tuesday, December 11, 2012

Obtaining custom configuration settings from a web.config for a Visual Web Part

If you're looking for a possible route for obtaining special configuration settings/information for a Visual Web Part that will be consistent across all sites contained under a given SharePoint Web Application, one possible route is to reference those configuration settings from the <appSettings> node of the web application's web.config file.  NOTE:  Only pursue this route if you know beyond a shadow of a doubt that the config settings will be consistent across all sites contained within the given web application.  If there is even a remote chance that you'll need to adjust those settings for each given site, you will wish to pursue another route for configuring your web part such as adding a custom property.  With that said, I'll now proceed with the steps you can use to make this possible:

Add the Custom Configuration Settings to the Web.config

Before you deploy you're web part, you will wish to open the web.config files for each of the SharePoint web applications under which your SharePoint web part will be deployed and add the new configuration setting to the <appSettings> node.  On your SharePoint Web Front End (WFE) server(s), the web applications will be located under the C:\inetpub\wwwroot\wss\VirtualDirectories directory and the web.config file will be located immediately under each given web application.  In this particular instance, I'm going to reference a custom setting called "dbConnectionString" which is presented as follows:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<configuration>
  <configSections>
  ...
  <appSettings>
    <add key="dbConnectionString" value="Data Source=dbServer;Initial Catalog=dbName;Integrated Security=True" />
  </appSettings>
</configuration>

After making the change, be sure to save the updated web.config file and, now, we move on to actually putting this info to good use...

Referencing the Custom Configuration Settings

To reference the custom configuration settings in your Visual Web Part, you will need to carry out the following steps:
  1. Open your Visual Web Part in Visual Studio
  2. In Solution Explorer, expand the Project tree, right-click on References and select Add Reference...
  3. Click on the .NET tab
  4. Locate and click on the System.Configuration component
  5. Click OK
  6. Open your Visual Web Part's .ascx.cs file
  7. Add the following code that will reference the custom configuration setting:
using System.Configuration; using System.Web.Configuration; ...
protected void Page_Load(object sender, EventArgs e) {     if (!IsPostBack)     {         string dbConnectionString = string.Empty;         string errorMessage = string.Empty;         try         {             using (SPSite site = SPContext.Current.Site)             {                 Configuration config = WebConfigurationManager.OpenWebConfiguration("/", site.WebApplication.Name);                 if (config.AppSettings.Settings["dbConnectionString"] != null)                 {                     dbConnectionString = config.AppSettings.Settings["dbConnectionString"].Value;                 }                 else                 {                     throw new Exception(string.Format("The 'dbConnectionString' <appSettings> node could not be located in the web application's, {0}, web.config file.", site.WebApplication.Name));                 }             }             ... Do something with the configuration info         }         catch (Exception ex)         {             ...Handle the exception properly         }     } }
At this point, you can now deploy your web part to sites contained under the web applications that you added the custom setting to (via updating the web.config file, of course) and they will reference the value that you've specified.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.