开发者

Accessing web.config from Sharepoint web part

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 appSetti开发者_Python百科ng 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜