通过编程方式添加XmlFormView控件到SharePoint页面

下面的代码片段中,我将在SharePoint 2010应用程序页上添加一个xmlformview控件,并使用code behind的方式在页面上加载xml表单,同时设置xmlformview控件的属性。

应用程序Aspx页面 

 

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”MyFormPage.aspx.cs”  Inherits=”MyFormPage”  EnableSessionState=”True” %>
<%@ Register tagprefix=”fv” namespace=”Microsoft.Office.InfoPath.Server.Controls”  assembly=”Microsoft.Office.InfoPath.Server, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c” %>
<%@ Register tagprefix=”Server” namespace=”Microsoft.Office.InfoPath” assembly=”Microsoft.Office.InfoPath, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c” %>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head runat=”server”>
<title>FormViews</title>
</head>
<body style=”margin: 0px;overflow:auto;padding:20px”>
<form id=”form1″ runat=”server” enctype=”multipart/form-data”>
<fv:XmlFormView ID=”formView” runat=”server” EditingStatus=”Editing” Width=”700px”>
</fv:XmlFormView>
</form>
</body>
</html>

请注意,为了能够正确显示表单,XmlFormView需要知道将要显示的表单的位置,展现该表单所使用的表单模板的位置,以及将表单保存到什么位置。属性XmlLoacation,XsnLocation和SaveLocation正是用于传递这些信息给XmlFormView对象。

现在,让我们看一下该应用程序页的后台代码。下面的后台代码通过On_Load方法来显示实际的InfoPath表单。在加载该表单前,Onload方法会读取查询字符串参数,如"action",来决定需要通过XmlLocation参数加载一个已有的表单,或者是应该通过XsnLocation分配一个模板(如果action==new).

后台代码

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.InfoPath.Server.Controls;
namespace formviewdemo
{
public partial class MyFormPage: LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.Params["action"];
if (action == “new”)
{
// Set the template location for new forms
String templateLib = “FormServerTemplates”;
String xsnName = “template.xsn”; ->> form template Name
formView.XsnLocation = String.Format(“{0}/{1}/{2}”, SPContext.Current.Web.Url, templateLib, xsnName);
}
else
{
// Set the XML location for an existing form ( in the sharepoint library)
String lib = “TestForms”;
String name = “example.xml”; ->> name of the infopathform
formView.XmlLocation = String.Format(“{0}/{1}/{2}”, SPContext.Current.Web.Url, lib, name);
}}
}}

 

参考资料

add xmlformview control on sharepoint page programmatically

posted @ 2010-08-29 18:13  Sunmoonfire  阅读(241)  评论(0)    收藏  举报