Accessing web.config from Sharepoint web part

Posted by philj on Stack Overflow See other posts from Stack Overflow or by philj
Published on 2010-06-15T01:50:22Z Indexed on 2010/06/15 2:02 UTC
Read the original article Hit count: 306

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about c#

Related posts about sharepoint2007