Read the web.config FeatureActivated event when a function is activated using PowerShell

In the FeatureActivated event (WebApplication scope) I want to read the web.config file to get the connection string from there. I use the following code to open web.config:

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

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

Can someone give me a workaround for this problem?

+4
source share
1 answer

I found a workaround for my requirement. Share it with you if this 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(); } 
+2
source

All Articles