SharePoint 2010 web.config : Read and Write values

How to read appSettings from SharePoint 2010 web.config file

1. Add reference to “System.Configuration.dll” file

2. Get Web application name.

3. To get current SharePoint site Web application name, use SPContext.Current.Site.WebApplicaiton.Name

4. Get Configuration object from WebConfigurationManager class

5. Get AppSettings object from Configuration object.

6. Get value

Code:
             string WebApplicationName = string.Empty;
             Configuration Config;
              AppSettingsSection AppSettings;  
             
             WebApplicationName = SPContext.Current.Site.WebApplication.Name;   
             Config = WebConfigurationManager.OpenWebConfiguration(“/”, WebApplicationName);
             AppSettings = Config.AppSettings;

             if (AppSettings != null)
             {
                        if (!string.IsNullOrEmpty(AppSettings.Settings[“Key”].Value))
                        {
                           // write code
                        }

              }

      Leave a Reply