2010-06-15 62 views
2

我有一個VS 2008 Web部件項目 - 這個項目是一個web.config文件: 是這樣的:從SharePoint Web部件訪問的web.config

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

在我的網絡的一部分,我想在appSetting部分訪問值: 我已經嘗試了所有下面的代碼,每個返回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()]; 

我試過這個代碼,我發現:

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

,並通過它踩在調試模式 - 即把事情一樣:

FeedCacheTimer
FeedPageURL
FeedXsl1
ReportViewerMessages

沒有從任何未來在我的web.config文件中... .may可以是sharepoint本身的配置文件嗎?我如何訪問web.config(或任何其他類型的配置文件!)本地到我的web部分?

感謝,

菲爾Ĵ

+0

@philj:格式化代碼,在編輯器中選擇它並按下Control-K。否則,它最好看起來很可怕。 – 2010-06-15 01:53:17

回答

1

這些數值看起來像默認的SharePoint的web.config性能。當您創建SharePoint Web部件時,它將部署到託管具有自己的(非常大)web.config文件的SharePoint站點的IIS虛擬目錄,而不是您的應用程序web.config文件。

根據您的生產環境,可以使用所需值和代碼應該工作的值更新SharePoint web.config文件。如果無法更新web.config文件,則可以使用部署到的IIS目錄中的獨立配置文件(自定義代碼然後閱讀它)或可能與每個SP網站集相關聯的屬性包進行查看需要通過代碼添加您的值。

+0

感謝您的信息。在我的情況下,它使用位於C:\ MOSS \ Webs \ Portal \ web.config的web.config,我認爲它是可以的。 找到訪問其他位於其他web.config文件(本地ot項目?)但沒有任何工作的代碼示例。 – user360943 2010-06-16 14:54:17