共用方式為


How to: Verify a Batch of Form Templates

Applies to: SharePoint Server 2010

For the purposes of this task, the VerifyFormTemplate method of the FormTemplateCollection class is used to verify a form template listed in a text box. This performs the same process as using the Verify button on the Upload Form Template page of the SharePoint 2010 Central Administration site.

The form that you create for this project contains the following controls: a FolderBrowserDialog control; a button which opens the FolderBrowserDialog; a text box that holds the location of the folder that contains the form templates to be verified; a button which executes the form template verification; and a rich text box to display the form template verified and any converter messages.

Note

This topic assumes that Microsoft Visual Studio is installed on the Web front-end (WFE) or single farm server that is running InfoPath Forms Services.

To set up the project

  1. Create a new Visual Basic Windows Forms Application project in Microsoft Visual Studio.

  2. On the Project menu, click Add Reference.

  3. On the .NET tab of the Add Reference dialog box, select Microsoft SharePoint Foundation, and then click OK. (If Microsoft SharePoint Foundation is not available on the .NET tab, on the Browse tab, browse to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\, select the Microsoft.SharePoint.dll assembly, and then click OK.

  4. On the Project menu, click Add Reference again.

  5. On the Browse tab of the Add Reference dialog box, browse to C:\Program Files\Microsoft Office Servers\14.0\Bin\, select the Microsoft.Office.InfoPath.Server.dll assembly, and then click OK.

To add controls and code to the form

  1. Add the following controls to the form. These can be found in the All Windows Forms category of the Visual Studio Toolbox:

    • Two Button controls

    • A TextBox control

    • A RichTextBox control

  2. Rename the first button to "Pick a Folder" and the second button to "Verify Form Templates" by modifying the Text property of each button in the Properties Window.

  3. Reposition and resize the form and the controls until all text can be seen on the buttons and the RichTextBox control fills most of the form.

  4. On the View menu, click Code.

  5. Paste the following code into the code window, replacing all existing code.

  6. Click Form1.vb [Design] on the Window menu.

  7. In the Properties Window, click the drop-down list, and then select Button1.

  8. In the Properties Window, click the Events button, which is typically the fourth button from the left in the row of buttons under the drop-down list.

  9. In the Action section, click the drop-down for the Click event, and then select Button1_Click.

  10. In the Properties Window, click the drop-down list, and then select Button2.

  11. In the Action section, click the drop-down list for the Click event, and then select Button2_Click.

  12. On the File menu, click Save All.

  13. Press F5 to run the application.

Example

Use the procedures earlier in this topic to create a new Visual Basic Windows application that uses the following code example to verify form templates in a folder, and to list any converter messages associated with each form template or messages indicating the form template is ready to be uploaded.

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

If you have to process every form template in the main folder and any subfolder, change the IO.SearchOption.TopDirectoryOnly parameter to IO.SearchOption.AllDirectories.

If you have to upload form templates after verifying them, use the UploadFormTemplate method.

See Also

Tasks

How to: Verify a Form Template Before Deployment

Other Resources

Developing Windows Applications to Perform InfoPath Forms Services Administration Tasks