InfoPath 2010 previous next navigation on a SharePoint list

Learn how you can create an InfoPath 2010 form template with previous and next buttons for navigation through items of a SharePoint list that has been added as a secondary data source to the form template.

Problem

You have a SharePoint 2010 list and you want to be able to navigate through the items in the list one by one by clicking on previous and next buttons.

InfoPath does not allow you to filter rows or sections that are bound to a repeating group in a secondary data source, as is the case when you create a receive data connection to a SharePoint list, so you must find a workaround.

Solution

You can create a solution that combines a hidden field, formulas, and conditional formatting to be able to filter data from a secondary data source for a SharePoint list and thereby create previous next navigation functionality in InfoPath 2010.

Discussion

To create previous next navigation in InfoPath 2010 on SharePoint list items:

  1. In InfoPath, create a new Blank Form template.
  2. Add an InfoPath 2010 SharePont list data connection. Ensure that the check box Automatically retrieve data when form is opened is selected when you create the data connection. This article uses a SharePoint 2010 list named OfficeApplications and has imported 3 columns named: ID, Title, and Color. The secondary data source for this SharePoint list is shown in the following figure.

    Secondary data source of a SharePoint list in InfoPath 2010

    Figure 1. Secondary data source of a SharePoint list in InfoPath 2010.

  3. Add 3 Text Box controls named id, title, and color to the InfoPath form template.
  4. Add 2 Button control to the form template and change their labels to Previous and Next, respectively.
  5. Add an InfoPath hidden field named position to the Main data source of the InfoPath form.
  6. Design an InfoPath form template similar to the one shown in the following figure.

    InfoPath 2010 form template for previous next navigation

    Figure 2. InfoPath 2010 form template for previous next navigation.
  7. Click the id text box to select it.
  8. On the Properties tab, click Default Value.
  9. On the Field or Group Properties dialog box, click the formula button behind the Value text box.
  10. On the Insert Formula dialog box, click Insert Field or Group.
  11. On the Select a Field or Group dialog box, select the secondary data source to the SharePoint list from the Fields drop-down list box, expand the dataFields node, select the ID node, and then click Filter Data.
  12. On the Filter Data dialog box, click Add.
  13. On the Specify Filter Conditions dialog box, select Select a field or group from the first drop-down list box.
  14. On the Select a Field or Group dialog box, select Main from the Data source drop-down list box, select the position field, and then click OK.
  15. On the Specify Filter Conditions dialog box, select The expression from the first drop-down list box and then change the expression in the text box from:

    xdXDocument:get-DOM()/my:myFields/my:position = ""
    to

    number(xdXDocument:get-DOM()/my:myFields/my:position)
    and click OK.
  16. On the Filter Data dialog box, click OK.
  17. On the Select a Field or Group dialog box, click OK.
  18. On the Insert Formula dialog box, select the Edit XPath (advanced) check box, and change the expression from

    xdXDocument:GetDOM("OfficeApplications")/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW/d:ID[number(xdXDocument:get-DOM()/my:myFields/my:position)]
    to

    xdXDocument:GetDOM("OfficeApplications")/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW[number(xdXDocument:get-DOM()/my:myFields/my:position)]/d:ID
  19. Click Verify Formula to check that the formula does not contain any errors, and then click OK.
  20. On the Field or Group Properties dialog box, click OK.
  21. Repeat steps 7 through 20 for the title and color fields. Tip: You can also copy the expression from step 18, change the field name in the expression from ID to Title and from ID to Color, and then paste it in the Default Value field for the title and color fields.
  22. Right-click the position field in the Main data source and select Properties from the drop-down menu.
  23. On the Field or Group Properties dialog box, enter a 1 in the Default Value text box, and click OK.
  24. Add an Action rule on the Previous button with an action that says:

    Set a field's value: position = position - 1
  25. Add an Action rule on the Next button with an action that says:

    Set a field's value: position = position + 1
  26. Add a Formatting rule on the Previous button with a Disable this control formatting and a condition that says:

    position = 1
  27. Add a Formatting rule on the Next button with a Disable this control formatting and a The expression condition that says:

    number(xdXDocument:get-DOM()/my:myFields/my:position) >= count(xdXDocument:GetDOM("OfficeApplications")/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW/d:ID)
    Tip: Copy the expressions you constructed earlier in steps 15 and 18. Remove the filter from the expression from step 18 and add the count() function around it. Then separate the two expressions with the >= sign. What the final expression does is check whether the value of the position field is greater than or equal to the total amount of items in the SharePoint list.
  28. Save the form template and test it.

Now when the form opens, the first item of the SharePoint list is shown and the Previous button is disabled.

Previous next navigation of a SharePoint list with InfoPath 2010

Figure 3. Previous next navigation of a SharePoint list with InfoPath 2010.

As you click the Next button and navigate through the items of the SharePoint list, the Previous button is enabled. When you reach the final item in the SharePoint list, the Next button is disabled. When you click and navigate backwards through the SharePoint list items, the Next button is enabled again, and once you reach the first item, the Previous button is disabled again.

 
 Subscribe for updates via RSS or Email

Related InfoPath Articles:





InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services