共用方式為


How to: Quiesce a Form Template Selected From a List

Applies to: SharePoint Server 2010

For this task, a DataGridView control is populated with a list of the form templates that have been uploaded to the server by an administrator. This list corresponds to the list found on the Manage Form Templates page, which is available under General Application Settings on the SharePoint 2010 Central Administration site, and does not include user-deployed form templates.

The form requires two buttons and a DataGridView control. The first button sets up the data grid and then calls a routine to populate it. The second button quiesces the form template selected in the data grid and then calls a routine to refresh the data grid. Quiescing a form template blocks new sessions from being started and allows current sessions to expire over a specified time frame. See the Quiesce method for more information about form template quiescing.

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 C# 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 DataGridView control

  2. Rename the first button to "List all form templates" and the second button "Quiesce form template" 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 DataGridView 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. Replace WindowsApplication1 in the namespace WindowsApplication1 line with the namespace of your application. Your namespace can be found in the Solution Explorer and Class View windows.

  7. Click Form1.cs [Design] on the Window menu.

  8. In the Properties Window, click the drop-down list and select button1.

  9. 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.

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

  11. In the Properties Window, click the drop-down list, and then select button2.

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

  13. On the File menu, click Save All.

  14. Press F5 to run the form and debug the code.

Example

Use the procedures earlier in this topic to create a new Visual C# Windows Forms application that uses the following code example to list form templates on a server that is running InfoPath Forms Services and quiesce a form template selected from the list.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.InfoPath.Server.Administration;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //setup DataGridView1
            dataGridView1.ColumnCount = 4;
            dataGridView1.SelectionMode =
            DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.MultiSelect = false;
            dataGridView1.Columns[0].Name = "Form Name";
            dataGridView1.Columns[1].Name = "Form ID";
            dataGridView1.Columns[2].Name = "Status";
            dataGridView1.Columns[3].Name = "Locale";
            //populate DataGridView1
            populateDataGrid();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            FormsService localFormsService;
            SPFarm localFarm = SPFarm.Local;
            
            try
            {
                localFormsService = localFarm.Services.GetValue<FormsService>(FormsService.ServiceName);
                //get the selected row
                DataGridViewSelectedCellCollection dgvsc = dataGridView1.SelectedCells;
                //get the FormID from the selected cells
                string fTempToQuiesce = dgvsc[1].Value.ToString();
                //quiesce the form template
                TimeSpan ftQuiesce = new TimeSpan(600000000); //1 minute
                localFormsService.FormTemplates.Item(fTempToQuiesce).Quiesce(ftQuiesce);
                //populate DataGridView1 again to show updated status
                populateDataGrid();
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message.ToString());
            }
        }

        private void populateDataGrid()
        {
            FormsService localFormsService;
            SPFarm localFarm = SPFarm.Local;

            //clear all rows if DataGridView1 has already been populated
            if (dataGridView1.Rows.Count > 0)
            {
                dataGridView1.Rows.Clear();
            }

            try
            {
                localFormsService = localFarm.Services.GetValue<FormsService>(FormsService.ServiceName);
                foreach (FormTemplate fTemplate in localFormsService.FormTemplates)
                {
                    //add each form template to the DataGridView1
                    dataGridView1.Rows.Add(fTemplate.DisplayName.ToString(), fTemplate.FormId.ToString(), fTemplate.QuiesceStatus.ToString(), fTemplate.Locale.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error occurred: " + ex.Message.ToString());
            }
        }
    }
}

See Also

Other Resources

Developing Windows Applications to Perform InfoPath Forms Services Administration Tasks