0
我有一個發佈網站與MOSS 2007構建,我需要以編程方式獲取頁面內容。我知道我可以使用WebClient向頁面發送請求,然後解析響應文本。但我想在SharePoint模型中實現,因爲規模很大。如何以編程方式呈現DataFormWebPart?
我想要呈現的Web部件是DataFormWebPart,如果在瀏覽器中查看它們會顯示正常。但是當我嘗試以編程方式呈現它們時,我收到了異常。
代碼:
var partMgr = siteCollection.RootWeb.GetLimitedWebPartManager(pageUrl, PersonalizationScope.Shared);
var sr = new StreamWriter(@"d:\temp\test.txt", false);
var htr = new HtmlTextWriter(sr);
foreach (WebPart part in partMgr.WebParts)
{
if (part.GetType() == new DataFormWebPart().GetType())
{
try
{
htr.WriteLine("");
part.RenderBeginTag(htr);
htr.WriteLine("");
part.RenderControl(htr);
htr.WriteLine("");
part.RenderEndTag(htr);
htr.WriteLine("");
}
catch (Exception exc)
{
htr.WriteLine("Message: " + exc.Message);
htr.WriteLine("StackTrace: " + exc.StackTrace);
htr.WriteLine("InnerException: " + (exc.InnerException == null).ToString());
}
}
}
sr.Close();
htr.Close();
輸出:
<div id="g_1722aa69_d0d7_4804_83fa_c8f4a250080a">
Message: Value cannot be null. Parameter name: page
StackTrace: at System.Web.UI.WebControls.WebParts.WebPartManager.GetCurrentWebPartManager(Page page)
at Microsoft.SharePoint.WebPartPages.WebPart.Render(HtmlTextWriter writer)
at ExtractPageData.Program.Main(String[] args) in D:\DNR-Playground\ExtractPageData\ExtractPageData\Program.cs:line 49
InnerException: False
<div id="g_1115045e_b0d7_41ce_ad19_15aab0f3871d">
Message: Value cannot be null. Parameter name: page
StackTrace: at System.Web.UI.WebControls.WebParts.WebPartManager.GetCurrentWebPartManager(Page page)
at Microsoft.SharePoint.WebPartPages.WebPart.Render(HtmlTextWriter writer)
at ExtractPageData.Program.Main(String[] args) in D:\DNR-Playground\ExtractPageData\ExtractPageData\Program.cs:line 49
InnerException: False
正如你在輸出看到的,標籤開始呈現,但控制不能呈現由於part.Page
是null
它是一個只讀屬性。