以编程方式创建浏览器启用的 InfoPath 表单






4.83/5 (5投票s)
如何通过编程方式创建浏览器启用的 InfoPath 表单。
引言
本文概述了使用代码创建、填充和上传 InfoPath 表单所需的步骤和代码。
设计 InfoPath 表单
使用 Microsoft InfoPath 设计器创建一个 InfoPath 表单。此示例表单将存储客户的姓名和姓氏,以及产品和金额的表格。

创建 InfoPath 表单的类表示
- 保存 InfoPath 表单
- 加载 Visual Studio 命令提示符
- 定位源文件
- 执行命令 xsd.exe /c /l:CS myschema.xsd。

图 2 - 另存为源文件


XSD 工具是 .NET Framework 工具的一部分。我们将使用它从 InfoPath 表单的 XSD 文件表示形式生成通用语言类。

图 5 – 生成的 myschema 类
// 
// This source code was auto-generated by xsd, Version=2.0.50727.1432.
// 
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,
 Namespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-02-04T09:20:42")]
[System.Xml.Serialization.XmlRootAttribute(
  Namespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-02-04T09:20:42",
  IsNullable=false)]
public partial class myFields {
private string nameField;
private string surnameField;
private ProductRow[] productTableField;
private System.Xml.XmlAttribute[] anyAttrField;.....
.....
.....
一旦文件在项目中可用,您就有能力将数据源绑定到 .NET 类,并以标准化的格式访问数据。如果您对 InfoPath 数据源进行任何更改,则需要重新运行 xsd.exe 命令,针对新的“schema.xsd”文件。
将表单模板上传到 SharePoint 表单站点

图 6 – 发布

图 7

图 8 – 选择 SharePoint Server

图 9

图 10

图 11

图 12

图 13
填充并上传表单数据
使用虚拟数据填充 myFields 类。该类是使用 XSD 应用程序创建的。
// Populate myfields class with dummy data. The class was created using the xsd
// application.
myFields fields = new myFields();
fields.Name = "Joe";
fields.Surname = "Blogs";
ProductRow[] rows = new ProductRow[2];
rows[0] = new ProductRow();
rows[0].ProductAmount = "1";
rows[0].ProductName = "Peach Jam";
rows[1] = new ProductRow();
rows[1].ProductAmount = "2";
rows[1].ProductName = "Strawberry Jam";
fields.ProductTable = rows;
// Create infopath form
MemoryStream myInStream = new MemoryStream();
// Form site used to store the generated forms
string rFormSite = @"http://sp/Sample Form";
// Location where form template is stored.
string rTemplateLocation = @"http://sp/Sample Form/Forms/template.xsn"; 
using (myInStream)
{
    // InfoPath forms are XML files. We have to write code to programmatically
    // generate the XML of the InfoPath form
    //Create XmlSerializer using myfields type
    XmlSerializer serializer = new XmlSerializer(typeof(myFields));
    XmlTextWriter writer = new XmlTextWriter(myInStream, Encoding.UTF8);
    // Insert infopath processing instructions
    string rInstruction = 
      "name=\"urn:schemas-microsoft-com:office:infopath:" + 
      "UploadSample:-myXSD-2009-02-04T09-20-42\"";
    rInstruction += 
        "solutionVersion=\"1.0.0.2\" productVersion=\"12.0.0.0\"
        PIVersion=\"1.0.0.0\" href=\"" + rTemplateLocation + "\"";
    // An important thing you need to make sure of is that the href attribute
    // in the mso-infoPathSolution processing instruction points to the correct
    // form template you published. 
    writer.WriteProcessingInstruction("mso-infoPathSolution", rInstruction);
    // Mark the xml as an infopath document. 
    writer.WriteProcessingInstruction("mso-application",
        "progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.2\"");
    // Serialize infopath data
    serializer.Serialize(writer, fields);
    // Upload form to form site.
    using (SPSite site = new SPSite(rFormSite))
    {
        // Create web instance.
        using (SPWeb spweb = site.OpenWeb())
        {
            // Add the file to the web file list.
            spweb.Files.Add(rFormSite + "\\" + Path.GetRandomFileName() + ".xml",
                myInStream.GetBuffer());
        }
    }                             
}
结果

图 14 – 表单库中的新项目

图 15 – 带有数据的表单加载

