We are developing an example application in five steps. We completed the first step in Part 1 and the second step in Part 2. Our task for today is step three, “Design the InfoPath Form.”

Here is a roadmap for the entire development process:

  1. Create a Search Application PageIn this step, we design the user interface for the page that the modal dialog displays, and we write code to perform the search.
  2. Add JavaScript for the Modal Dialog.  In this step, we create a JavaScript file with code to open the search application page in a popup modal dialog. The function that opens the modal dialog specifies a callback function that returns information about the document that the user has selected to the page that opened the dialog.
  3. Design the InfoPath Form.  In this step, we create a form template that includes a Search button, and we write form code that notifies the form's host when a user clicks the button.
  4. Create a Web Part to Host the Form.  In this step, we write a custom Web Part that uses an XMLFormView control to host the InfoPath form.
  5. Connect the Components. In this step, we bolt everything together. We write Web Part code that handles the NotifyHost event, code that invokes the modal dialog, and code that uses the dialog result to update the InfoPath form.

We will cover each step as a separate post.

Step 3: Design the InfoPath Form

At this point, we have a search application page, and we can display the page in a modal dialog. Now it is time to create a form to invoke the dialog.

For the example application, we do not need anything very complicated. A very simple form will do. It needs just three elements:

  • A label ("Organization Chart")
  • A hyperlink field ("URL")
  • A button ("Search")

We also want to create a handler for the button click event. In the handler, we call the XmlForm.NotifyHost method, and in the method's notification parameter we pass an XPath expression for the field. This expression is communicated via event args to whoever is handling the NotifyHost event on the host. (We will implement the host's side of the conversation later, in Step 5.)

Finally, we finish Step 3 by publishing the form to a website running SharePoint Server.

To design the form
  1. In Microsoft InfoPath Designer 2010, click the File tab. Then click New.

  2. Under Available Form Templates, select Blank Form. Then click Design Form.

  3. Click Click to add title, and then type "Contoso Resource Planning".

  4. In the body of the form, click Add tables. Then click the Insert tab and Custom Table. Insert a 3x1 table (three columns and one row).

  5. Click in the first column of the table and type "Organization Chart".

  6. Click anywhere in the second column of the table. Then click the Home tab, and in the Controls group under Objects click Hyperlink.

  7. In the Fields task pane, right-click field1 and choose Properties.

  8. On the Data tab, change the Name property to "URL". Click OK.

  9. Repeat the two previous steps, this time renaming field2 to "Title".

  10. In the second column of the table, right click the hyperlink control and choose Hyperlink Properties.  In the Hyperlink Properties dialog, check Read-Only. Then click OK.

    This step prevents the user from editing the hyperlink manually.

    image13

  11. Click anywhere in the third column of the table. Then click the Home tab, and in the Controls group under Objects click Button.

  12. Right-click the Button control and choose Button Properties.

  13. In the Button Properties dialog, change the Label property to Search and the ID property to InvokePopup. Then click OK.

  14. (Optional) Customize the page layout and form style as desired.

  15. On the File tab, click Save. Name the file ContosoResourcePlanning.xsn.

image14

 
To add a handler for the button click event
  1. In InfoPath Designer, click the Developer tab, and then click Language.

  2. In the Form Options dialog, under Form template code language, select C#. Click OK.

  3. On the Developer tab, click Code Editor to start Microsoft Visual Studio Tools for Applications.

  4. In InfoPath Designer, on the form design surface, select the Search button. Then c\Click the Properties tab on the ribbon. Click Custom Code to generate the click event handler for the button.

  5. In Visual Studio Tools for Application, locate the InvokePopu_Clicked method. Paste the following code after the comment:


    NotifyHost("/my:myFields/my:URL");

    This line of code serves two purposes. First, it notifies the host that a user has clicked the Search button. Second, it passes the XPath for the field that should receive the result of the search.

    Note: You can obtain the XPath of the URL field by right clicking the URL node in the Fields task pane and selecting Copy XPath.

  6. On the File menu, click Save FormCode.cs. Then exit Visual Studio Tools for Applications.

Now we are ready to publish the form template.

To publish the form template
  1. In InfoPath Designer, click the File tab.

  2. In the actions column, click Publish and then click SharePoint Server.

    image15

  3. In the Publishing Wizard, type the URL of the target SharePoint website and then click Next.

  4. Verify that Enable this form to be filled out by using a browser is checked and that Form Library is selected.

    image16

  5. Click Next twice.

  6. Click Publish.

    InfoPath Designer first creates a new form library on the website and then publishes the resource planning form in the library.

  7. In the Publishing Wizard, click Open this form in the browser.

  8. Copy the entire contents of the browser's address bar. Then paste the URL in a text file.

    The address bar contents should like something like this:

    http://teamsite/_layouts/FormServer.aspx?XsnLocation=http://teamsite/ContosoResourcePlanning/forms/template.xsn&OpenIn=browser&SaveLocation=http://teamsite/ContosoResourcePlanning&Source=http://teamsite/ContosoResourcePlanning
  9. Delete everything up to and including the first equals sign. Then delete everything after "template.xsn".

    The result should like something like this:

    http://teamsite/ContosoResourcePlanning/forms/template.xsn

    This is the absolute URL for the form's XSN file. You will need this information later in the application development process.

  10. Save the text file for later reference.

Next Steps

In Part 4 we create a custom Web Part to host the InfoPath form.