如何以编程方式呈现 DataFormWebPart?
我有一个使用 MOSS 2007 构建的发布网站,我需要以编程方式获取页面内容。我知道我可以使用 WebClient 向页面发送请求,然后解析响应文本。但我想在 SharePoint 模型中执行此操作,因为规模非常大。
我想要呈现的 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
并且控件无法呈现它是一个只读属性。
I have a publishing website build with MOSS 2007 and I need to get the page content programmatically. I know that I can use WebClient to sent a request to the page and then parse the response text. But I want to do it in SharePoint model since the scale is quiet large.
The web parta I want to render are DataFormWebPart and they display fine if viewed in a browser. But I'm getting exceptions when trying to render them programmatically.
The code:
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();
The output:
<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
As you can see in the output, the begin tag is rendered but the control can't be rendered due to part.Page
is null
and it it's a readonly attribute.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这根本行不通。 Web 部件需要正确的
SPContext
,它是在 ASP.NET 管道中处理 HTTP 请求期间设置的。如何克服它没有简单的答案。您本质上必须模拟对需要在内存中呈现的页面的 HTTP 请求。此外,此表达式
part.GetType() == new DataFormWebPart().GetType()
也远非理想。而是使用part.GetType() == typeof(DataFormWebPart)
来防止实例化和可能出现的不需要的副作用。This just won't work. Web parts require proper
SPContext
which is setup during HTTP request handling in the ASP.NET pipeline. There's no easy answer how to overcome it. You would essentially have to simulate an HTTP request to the page that you need to render in-memory.Also, this expression
part.GetType() == new DataFormWebPart().GetType()
is far from ideal. Rather usepart.GetType() == typeof(DataFormWebPart)
to prevent instantiation and possible unwanted side effects.