如何:验证一批表单模板

上次修改时间: 2010年3月30日

适用范围: SharePoint Server 2010

为执行此任务,可使用 FormTemplateCollection 类的 VerifyFormTemplate 方法验证文本框中列出的表单模板。这与使用"SharePoint 2010 管理中心"网站中"上载表单模板"页的"验证"按钮的结果是一样的。

您为该项目创建的表单包含以下控件:一个"FolderBrowserDialog"控件、一个用于打开"FolderBrowserDialog"的按钮、一个指示要验证的表单模板的文件夹位置的文本框、一个执行表单模板验证的按钮,以及一个显示经验证的表单模板和所有转换器消息的格式文本框。

备注

本主题假定 Web 前端 (WFE) 或运行 InfoPath Forms Services 的单个场服务器上安装了 Microsoft Visual Studio。

设置项目

  1. 在 Microsoft Visual Studio 中创建一个新的 Visual Basic"Windows 窗体应用程序"项目。

  2. 在"项目"菜单上,单击"添加引用"。

  3. 在"添加引用"对话框的".NET"选项卡上,选择"Microsoft SharePoint Foundation",然后单击"确定"。(如果".NET"选项卡上没有"Microsoft SharePoint Foundation",请在"浏览"选项卡上,浏览到 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\,选择 Microsoft.SharePoint.dll 程序集,然后单击"确定"。)

  4. 在"项目"菜单上,再次单击"添加引用"。

  5. 在"添加引用"对话框的"浏览"选项卡上,浏览到 C:\Program Files\Microsoft Office Servers\14.0\Bin\,选择 Microsoft.Office.InfoPath.Server.dll 程序集,然后单击"确定"。

向表单中添加控件和代码

  1. 将下列控件添加到表单中。在 Visual Studio"工具箱"的"所有 Windows 窗体"类别中可以找到这些控件:

    • 两个"Button"控件

    • 一个"TextBox"控件

    • 一个"RichTextBox"控件。

  2. 通过在"属性窗口"修改各个按钮的"Text"属性,将第一个按钮重命名为"选择文件夹",将第二个按钮重命名为"验证表单模板"。

  3. 重新定位表单和控件,并调整其大小,直到在按钮中能看到全部文本且"RichTextBox"控件填充表单的大部分。

  4. 在"视图"菜单上,单击"代码"。

  5. 将以下代码粘贴到代码窗口中,以替换所有现有代码。

  6. 在"窗口"菜单上单击"Form1.vb [Design]"。

  7. 在"属性窗口"中,单击下拉列表框,然后选择"Button1"。

  8. 在"属性窗口"中,单击"事件"按钮,此按钮通常是下拉列表下方按钮行中的左边第四个按钮。

  9. 在"操作"部分,单击"Click"事件的相应下拉框,然后选择"Button1_Click"。

  10. 在"属性窗口"中,单击下拉列表,然后选择"Button2"。

  11. 在"操作"部分,单击"Click"事件的相应下拉列表,然后选择"Button2_Click"。

  12. 在"文件"菜单上单击"全部保存"。

  13. 按 F5 运行应用程序。

示例

使用本主题前面的步骤新建一个 Visual Basic Windows 应用程序,在该应用程序中使用以下代码示例验证某个文件中的表单模板,并列出与各个表单模板相关联的所有转换器消息或指示表单模板已准备好上载的消息。

Imports Microsoft.SharePoint.Administration
Imports Microsoft.Office.InfoPath.Server.Administration

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Show the folder browser dialog
        If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim Directory As New IO.DirectoryInfo(TextBox1.Text)
        Dim AllFiles As IO.FileInfo() = Directory.GetFiles("*.xsn", IO.SearchOption.TopDirectoryOnly)
        Dim MyFile As IO.FileInfo
        Dim StrLog As String = ""
        Dim LocalFormsService As FormsService
        Dim LocalFarm As SPFarm
        Dim VerifyMessages As New ConverterMessageCollection
        Dim ConverterMsg As ConverterMessage
        Dim IntFileCount As Int16 = 0

        Try
            'Loop through each file
            For Each MyFile In AllFiles
                'Write the filename and path to the string
                StrLog = StrLog + MyFile.FullName.ToString() + Environment.NewLine
                LocalFarm = SPFarm.Local
                LocalFormsService = LocalFarm.Services.GetValue(Of FormsService)(FormsService.ServiceName)
                'Verify the form template
                VerifyMessages = FormTemplateCollection.VerifyFormTemplate(MyFile.FullName.ToString())
                'If there are no messages, display a message that the form template
                'is OK, otherwise loop through the messages and build the string
                If VerifyMessages.Count = 0 Then
                    StrLog = StrLog + "  There are no problems with this form template." + Environment.NewLine
                Else
                    For Each ConverterMsg In VerifyMessages
                        StrLog = StrLog + "  " + ConverterMsg.ShortMessage.ToString() + _
                        ": " + ConverterMsg.DetailedMessage.ToString() + Environment.NewLine
                    Next
                End If
                'Write the string to the rich text box
                RichTextBox1.Text = RichTextBox1.Text + StrLog
                RichTextBox1.Refresh()
                'Reset the string, adding a blank line between files
                StrLog = Environment.NewLine
                'Increment the file count
                IntFileCount = IntFileCount + 1
            Next
            'Show message that the files are done
            MessageBox.Show(IntFileCount.ToString() + " file(s) verified")
        Catch ex As Exception
            MessageBox.Show("An error occurred: " + ex.Message)
        End Try
    End Sub
End Class

如果您必须处理主文件夹或任何子文件中的各个表单模板,请将 IO.SearchOption.TopDirectoryOnly 参数更改为 IO.SearchOption.AllDirectories。

如果必须在验证后上载表单模板,请使用 UploadFormTemplate 方法。

请参阅

任务

如何:在部署前验证表单模板

其他资源

开发 Windows 应用程序以执行 InfoPath Forms Services 管理任务