Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm working on a project in SP/VS2010 that contains features with event receivers that require access to appsettings keys in the web application's web.config.

While I've checked that the features can acces these keys when (e.g.) activated from the web UI, they do not have access when activated via VS's Build > Deploy Solution menu item or via powershell.

I've established that this is because when the featre is activated by VS or Powershell, the call to ConfigurationManager.AppSettings in the feature receiver does not correspond to the web application's web.config. Instead, it corresponds to vssphost.exe's .config or powershell.exe's .config respectively.

While I can probably throw together a Powershell script to copy the required keys to powershell.exe.config before I try to use these features via PS, I can't think of a neat way to do copy the keys to vssphost4.exe.config every time I use Build > Deploy Solution in VS2010.

I can't be the only person with this issue, but I've been unable to find anything via Google. Is there a standard way to do this?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

I would recommend NOT to store appsettings in the web.config - use any other methods. SPWebConfigModifications are not reliable in all scenarions (just Bingle it and you'll find a plethora of posts about it).

For instance the hierarchichal configuration storage in the P&P SharePoint guidance is a great way to store settings (http://spg.codeplex.com)

share|improve this answer
    
+1 for not using appsettings. Alternative to configuration manager is to simply use properties on SPFarm or SPWeb (rootweb) –  Anders Rask Aug 3 '11 at 16:23
    
I agree with @wictor that you should minimize the altering of the web.config file. You could however either use the P&P way or write your own "settings provider" that reads from a app.config file in let's say a feature directory. I've made some test with an approach like this (johanleino.wordpress.com/2011/01/06/…) –  Johan Leino Aug 8 '11 at 10:42
    
I've used SPWebConfigModifications for several solutions now and have yet to encounter a problem with it. Custom, list based settings providers are the way to go for basic settings, but there are times where you will need to update the web.config (i.e. adding custom HttpHandlers or HttpModules, adding custom configuration for other libraries, etc.). –  Charles Chen Aug 22 '11 at 17:42
    
I also agree with @victor. We use the property bag to store settings. But what if you use third party components that use the config file? In our case we use the enterprise library for logging. In the web.config we have a entry pointing to a specific enterprise library config file. But this one entry in the web.config is necessary - as far is I know. –  tschuege Jan 9 '13 at 9:52

If you're modifying the web.config in a feature, the recommended practice is using SPWebConfigModification. This way, changes are kept in sync across all your web front ends.

Read this MSDN article on using the SPWebConfigModification: http://msdn.microsoft.com/en-us/library/bb861909.aspx

share|improve this answer
    
This is very helpful, as I hadn't really worried yet about how I was going to automate getting the changes into the application's web.config, but I'm specifically asking how to automate getting them into vssphost4's config for VS to use in deploying. –  Rawling Aug 3 '11 at 8:30
    
Note that SPWebConfigModification has a number of caveats and will not always be promoted/demoted from all web.config on all WFE's –  Wictor Wilen MCA MCM MVP Aug 3 '11 at 13:26

If you do want to use web.config you must point to the web.config file yourself, when running from powershell/stsadm because in that case you do not have a web context. You might be able to do this by accessing the SPWebApplication from the feature parent and determine its physical folder on the server. Than you could load the web.config using Linq To Xml and query for the wanted appSetting.

Another approach could be not use a feature at all but rather a command line tool.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.