One very useful feature of Microsoft SharePoint is its ability to create and maintain lists that users can leverage to store, view, and share items with other users of the SharePoint site. It is often desirable to be able to view these items and to add additional items with an InfoPath form; one such example is a list of contacts. In this example we will add items to the automatically generated Contacts list in SharePoint, but this technique can be used to add list items to any SharePoint list.
In this task we will add three data connections to a form: a connection to a SharePoint List to view the Contacts list, a connection to the SharePoint lists.asmx Web Service to submit new Contacts to, and a secondary XML document that we will use as a Collaborative Application Markup Language (CAML) template. Let’s start by designing a new blank form.
Add the CAML template:
Copy the following code into a text editor, and then save the file as Add List Item Template.xml:
<?xml version="1.0" encoding="UTF-8"?> <Batch OnError="Continue"> <Method ID="1" Cmd="New"> <Field Name='Title'></Field> <Field Name="FirstName"></Field> <Field Name="Email"></Field> <Field Name="WorkPhone"></Field> </Method> </Batch> |
Now that the CAML template has been saved, let’s design the form.
Add the CAML template as a secondary data source:
- Design a new blank form.
- Choose Data Connections from the Tools menu, and then click Add.
- In the Data Connection Wizard, select Receive Data, and then click Next.
- Select XML Document, and then click Next.
- Click Browse, locate and select the Add List Items Template.xml file, click Open, and then click Next.
- Click Finish, click Yes, and then click Close.
Add the SharePoint data connection to receive data:
- Choose Data Connections from the Tools menu, and then click Add.
- In the Data Connection Wizard, select Receive Data, and then click Next.
- Select SharePoint Library or List, and then click Next.
- Type the name of your SharePoint site into the text box, and then click Next.
- Select Contacts from the list of libraries, and then click Next.
- Select the Last_Name, First_Name, E-mail_Address, and Business_Phone fields, deselect every other field, and then click Next.
- Click Finish.
SharePoint stores lists using a unique identifier, called a GUID, for each list. You must know the GUID for your specific implementation of SharePoint in order to submit new items to it. This concept is also true for the field names represented in the CAML file and if you decide to create a new CAML template with more fields (or for a different list) you will have to follow a similar process to obtain the internal SharePoint field name for the particular columns.
Obtain the Contacts list GUID:
- Open Internet Explorer, log on to your SharePoint site, and browse to the Contacts list.
- Click Modify Settings and Columns on the left side of the browser window.
- Look at the URL in the Address Bar and copy the bracketed value of the List variable at the end of the URL (see Figure 1). Paste this text into a text editor (such as Notepad) for later use.
Figure 1. Obtaining the List name.
Create the default view:
- Switch back to the InfoPath Designer, and then open the Controls Task Pane.
- Insert a button into the view, and then double-click the new button.
- Select Refresh from the Action drop-down list, select One Secondary Data Source in the Refresh dialog box, and then select Contacts from the drop-down list.
- Click OK two times, and then type Enter a few times to insert some white space below the refresh button.
- Open the Data Source task pane, and then choose Contacts from the Data Source drop-down list.
- Right-click the myFields/dataFields/Contacts node and choose repeating table.
- Type Enter a few times to insert some white space.
- Open the Controls task pane, and then select Text Box to insert a new text box field into the view.
- Double-click this text box, rename to ListName, and then copy and paste the GUID of the Contact List into the Default Value field. (Refer to Figure 2).
- Click OK.
Figure 2. Adding the ListName text box.
We now have everything we need to add the data connection to the SharePoint web service, let’s do that now.
Add the connection to the SharePoint web service:
- Choose Data Connections from the Tools menu, and then click Add.
- Select Submit Data, click Next, select To a Web Service, and then click Next.
- Type the location of the lists.asmx file on your SharePoint site. This is usually located at http://sharepointserver/_vti_bin/lists.asmx.
- Click Next.
- Select UpdateListItems from the list, and then click Next.
- For the s0:listName parameter assign the /my:myFields/my:ListName node as the value.
- Highlight the s0:updates parameter, click the data source button, select Add List Item Template from the data source drop-down list, highlight the /Batch node, and then click OK.
- Choose XML Subtree, Including Selected Element from the Include drop-down list, and then click Next.
- Click Finish.
Figure 3. Assigning the Web Service variables.
Now that all of the data connections are properly configured we can finish designing our form.
Add the submit section:
- Open the Data Source task pane, and then choose Add List Item Template from the Data Source drop-down list.
- Right-click the /Batch/Method/Field node, and then choose Repeating Table.
- Double-click the text box control in the Name column, switch to the Display tab, select Read-Only, and then click OK.
NOTE: This field must be read-only because if it changes, our CAML will be affected and the submit will fail.
- Resize the columns to match Figure 5.
- Type Enter a few times to enter some white space.
- Open the Controls task pane, and then insert a new button.
- Double-click the button, select Submit from the Action drop-down list, Select Enable Submit Commands and Buttons, select Web Service from the Submit To drop-down list, and then select Submit from the data connection drop-down list.
- Click OK two times.
Figure 4. Adding the submit button.
Figure 5. The completed view.
Try it out:
- Click the Preview Form button (or type Alt+P).
- Click the Refresh button to obtain the list of Contacts already on the SharePoint site.
- Type a new Contact into the four field rows, and then click the Submit button.
- Click the Refresh button again to observe the new contact that has been added.
- Open Internet Explorer.
- Browse to the Contacts list on your SharePoint site and verify that a new contact has indeed been added.