Adding the List Data to a Task Form in InfoPath 2010
Viewing Initiation Lists Data as a Secondary Data connection on a Task Form using InfoPath 2010 in SharePoint 2010
- Building business processes in SharePoint usually requires assigning tasks at stages for users to approve, enter data, or move the process along somehow.
- Problem: Assigning a task to someone creates another object, separate from the original list or document library, which can confuse users.
- Solution: View external data, like the list that fired the workflow, inside the form that is created for a task. This is accomplished in InfoPath 2010.
- SharePoint Server 2010
- InfoPath 2010
- SharePoint Designer 2010
- Ensure that Workflow Features are Enabled in your site
This is an example of how to accomplish this. I am keeping this simple as you can adjust this later for your needs.
- Use a list in SharePoint 2010. If you need to create a custom list in a test area and populate it with some fields. We are only going to be using Title and ID in this example, again simple. I created a Custom List and named it: "SampleList".
- Open the site where this list exists in SharePoint Designer 2010.
- Create a List Workflow on this list. I named mine: "ApprovalTest".
- In the first step, click Action -> Start Approval Process.
Figure 1: Adding the Approval Workflow Process
- Assign it to yourself. Give the email notification a Subject and a Body, but you can put a word in each for simplicity.
- Publish the Workflow.
- Go to All Files -> Workflows and open the folder for the workflow. It should look like Figure 2.
Figure 2: These are the actual files for a workflow.
- Click on the task form, in this case it is Approval.xsn. (The name will match whatever you call the Action added in step 4, shown as: Start Approval process on Current Item with these users)
- Click Edit File, see Figure 3.
Figure 3: Editing an InfoPath from in a Workflow
- Open the Data menu in the ribbon. Choose From SharePoint List in the Get External Data section.
- Enter the site you created the list in.
- Select the list. In this case it is SampleList. Click Next.
Figure 4: Select the List/Library you want to Bring data in from
- Select the Fields you want to have access to. For this example we only need to add Title. Sort can be left as ID, because we will be filtering the results by a single ID for the row that fires this Approval Process, so sorting on 1 item doesn't matter. Click Next.
- Do not select the checkbox. Storing a copy of the data in the form template for a SharePoint list or library would be slow. Click Next.
- I would leave the name for the data connection alone. UNCHECK the box for: Automatically retrieve data when form is opened. (We will create a rule on form load that queries the data for us so we can choose to filter it by ID). Click Finish.
- In the ribbon, Select Form Load in the Rules section.
Figure 5: Form Load Rule
- Click New -> Action.
- Under the Details for: (enter a good name), I use "Load SampleList Data". See Figure 6.
- Under Run these actions, select Add -> Set a fields data. See Figure 6.
Figure 6: Rules Settings
- Click on the field button menu at the right (see Figure 7), in this case we need to set the ID of the Secondary Data Source we added, SampleList.
Figure 7: Select the Field menu.
- The next window pops up, select advanced view at the bottom. Select your data connection in the drop-down menu at the top. In this case it is SampleList (Secondary). Choose the queryFields node, then ID (see Figure 8). This will allow us to set the ID field using information from the current Task. Click OK.
Figure 8: Choose the ID field.
- Next we need to select the Value, by clicking on the Fx function button on the right. (see Figure 9)
Figure 9: Value and Function settings.
- This brings up a Formula window. Select Insert Function... button (see Figure 9), substring-after. Click OK. The format of a URL in an item in a SharePoint list is http://yoursite/lists/yourlistname/dispform.aspx?ID=_ The ID is what we’re looking for, so the substring after the equals sign can be extracted and compared to the ID field in the list.
Figure 10: The formula we will use to Set the ID.
- Notice in Figure 10 we can double-click the fields, click the first and select Advanced View, Make sure the datasource drop-down is on Main, select the dataFields node, then Related Content. After you select the Related Content field and click OK, it calls it WorkflowLink. Related content is a link back to the item this task is related to, and it is a URL with the ID for that item at the end. We will be stripping away everything but the ID.
Figure 11: Selecting the Related Content field.
- Replace the second parameter in the substring-after formula to "=". It should look like Figure 12.
Figure 12: Completed Formula
- Click OK on the formula window. Then click OK on the Rule Details window.
- On the Rules menu, Select the Add drop-down in the Run these actions section. Choose Query for Data.
- In the Rules Detail popup window, in the Data connection, choose your list secondary connection. In this case it is "SampleList". Click OK.
- Now we have 2 rules. One that sets the ID field of the data connection we added to the ID of the item this task is related to. Another that will query that data connection to bring in the fields.
- Edit the form to display the Fields from the list item. The best way is to add a Calculated Value field from the Home ribbon menu Controls section, and point it to the queryField. A popup menu appears, called Insert Calculated Value. Click on the Fx function button. Click the Insert Field or Group... button. (see Figure 13)
Figure 13: Insert Calculated Value windows.
- Choose the field you would like. I will be adding the Title and ID fields. (see Figure 14) Make sure for the calculated fields you use the dataFields node and not the queryFields node. Click OK twice. Repeat for all fields that you want to bring in.
Figure 14: Select the Calculated Field from your Secondary Data Connection.
- You can customize the look of the value field by adding a border with the Layout menu in the ribbon and right-clicking it and choosing Properties to format the value as currency, number, etc.
- I customize the form to show things better. Figure 15 below shows my sample form for this task.
Figure 15: Completed Form with some Formatting.
- Publish the form. Then open up the list and create a sample item. I will Title it Test Item. Click Save.
Figure 16: Add a new item in the SharePoint list.
- Next we need to run our ApprovalTest workflow on this item, which I left as start manually.
Figure 17: Run the ApprovalTest workflow.
- Click Start to start the workflow.
- I should get an email notification that the workflow has started and another email telling me a task is assigned to me to approve this item.
- Navigate to the Tasks list the workflow is using and open the task to verify the form is showing the details from the list inside the Task form.
Often times I create a list for a business process and then associate documents in a library using a lookup column. I will also be testing whether or not I can view these documents in the task form as well, since in several case these documents are being approved as well as the list item (the list item is really the details and tracking the process of the documents).