4
votes

On FeatureActivated event(WebApplication scoped) I want to read the web.config to get from there a connection string. I use the following code to open the web.config:

Configuration rootWebConfig = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);

Everything works fine when I activate the feature through the web interface, but when I try to do the same through PowerShell(Enable-SPFeature) the code fails. From what I saw, the code opens a wrong web.config from the path C:\inetpub\wwwroot\web.config(which does not exist) instead of C:\inetpub\wwwroot\wss\VirtualDirectories\\web.config.

Can some one give me any workaround for this problem?

1
I am facing the same issue. Did you find a resolution?Web User

1 Answers

2
votes

I found a workaround for my requirement. Sharing it with you, in case it helps:

        using (SPWeb web = properties.Feature.Parent as SPWeb)
        {
            SPWebApplication webApp = web.Site.WebApplication;
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApp.Name);
            // App settings are retrieved this way
            config.AppSettings.Settings["someAppSettingKey"].Value;

            // Connection string settings are retrieved this way
            config.ConnectionStrings.ConnectionStrings["someDBConnectionString"].ConnectionString
            web.Site.Dispose();
        }