views:

179

answers:

1

I have a VS 2008 web parts project - in this project is a web.config file: something like this:

<?xml version="1.0"?>
<configuration>
  <connectionStrings/>
  <system.web>
    <appSettings>
      <add key="MFOwner" value="Blah" />
    </appSettings>
…….

In my web part I am trying to access values in the appSetting section: I've tried all of the code below and each returns null:

 string Owner = ConfigurationManager.AppSettings.Get("MFOwner");
 string stuff1 = ConfigurationManager.AppSettings["MFOwner"];
 string stuff3 = WebConfigurationManager.AppSettings["MFOwner"];
 string stuff4 = WebConfigurationManager.AppSettings.Get("MFOwner");
 string stuff2 = ConfigurationManager.AppSettings["MFowner".ToString()];

I've tried this code I found:

NameValueCollection sAll;
sAll = ConfigurationManager.AppSettings;
string a;
string b;
foreach (string s in sAll.AllKeys)
   {
     a = s;
     b = sAll.Get(s);
    }

and stepped through it in debug mode - that is getting things like :

FeedCacheTimer
FeedPageURL
FeedXsl1
ReportViewerMessages

which is NOT coming from anything in my web.config file....maybe a config file in sharepoint itself? How do I access a web.config (or any other kind of config file!) local to my web part???

thanks,

Phil J

+1  A: 

Those values look like default SharePoint web.config properties. When you create a SharePoint web part, it gets deployed to the IIS virtual directory that hosts the SharePoint site which has it's own (very large) web.config file and NOT your application web.config file.

Depending on your production environment, you can update the SharePoint web.config file with the values you want and your code should work. If you cannot update the web.config file you can look into using a stand alone config file in the IIS directory that you deploy to (custom code then to read it) or possibly the property bag associated with each SP site collection for which you would need to add your values via code.

John Ptacek
Thanks for the info. In my case it is using the web.config located at C:\MOSS\Webs\Portal\web.config, which I suppose is ok.Found code examples of accessing other web.config files located elsewhere(local ot project?) but none worked.