Copy a SharePoint list to a repeating table in InfoPath using XSLT
Learn how to copy data from a SharePoint list to a repeating table in InfoPath by using XSLT and C# code to transform the data from a secondary data source and store it in the Main data source.
You have a SharePoint list that contains a date column and you want to be able to copy the data from the SharePoint list to a repeating table on an InfoPath form.
SharePoint date columns once included in an InfoPath secondary data source become strings that have a different date format than the date formats used in InfoPath. And since the nodes of the secondary data source to the SharePoint list are locked once the SharePoint list is bound to InfoPath, you are unable to change the data type on the fields of the SharePoint list from within InfoPath.
Use an XSLT to transform the SharePoint date format into a format that InfoPath recognizes as a date and copy the entire contents of the SharePoint list to a repeating table in InfoPath.
You can copy data from a SharePoint list to a repeating table in InfoPath using XSLT and C# code as follows:
- In SharePoint, use an existing SharePoint list or create a new one that has a date column in it.
Here we'll use the SharePoint list displayed in the following figure:
Figure 1. SharePoint list containing a date column.
- In InfoPath, create a new Blank form template.
Add a data connection to the SharePoint list from step 1. Here we'll select the Title, FullName, and DueDate fields to be included in the data connection, and call the data connection SimpleList.
The XML for the secondary data source to the SharePoint list in InfoPath resembles the following:
Figure 2. The XML of a secondary data source to a SharePoint list in InfoPath.
Note: You can retrieve the XML of the secondary data source by using the following C# code:
string xml = DataSources["SimpleList"].CreateNavigator().OuterXml;
This is the XML that you'll have to transform; the source XML. Note how the dates are displayed in the format yyyy-MM-dd HH:mm:ss. You'll have to transform these dates into an InfoPath date format such as yyyy-MM-dd and that can be used in a Date Picker control.
Add a Repeating Table control to the InfoPath form template and change the field in the third column of the repeating table into a Date Picker control in the repeating table.
The XML for the repeating table resembles the following:
Figure 3. The XML of a repeating table in InfoPath.
Note: You can retrieve the XML for the repeating table in the Main data source of the InfoPath form template by using the following C# code:
string xml = MainDataSource.CreateNavigator().SelectSingleNode(
This is the XML that you'll have to create; the destination XML. Note that the repeating table is empty at this point in time and by default contains only one row. group2 is the node that repeats. You'll have to copy Title from the SharePoint list to field1, FullName to field2, and DueDate to field3.
- Construct an XSLT stylesheet as shown in Figure 4, save the file as transform.xsl, and add it as a Data Connection
(select Receive data and XML document in the data connection wizard when creating the data connection) to the InfoPath form and name the secondary data source transform:
Figure 4. XSLT used to transform data from the SharePoint list into data for the repeating table in InfoPath.
This XSLT loops through the SharePoint list and creates a group2 for each item in the SharePoint list. While looping through the SharePoint list, it copies the values from the Title, FullName, and DueDate SharePoint list columns into the field1, field2, and field3 nodes of the group2 node.
Note: This XSLT uses the substring() XSLT function on the DueDate field in the SharePoint list to extract the first 10 characters from the date string. Furthermore, the dfs namespace in the XSLT was copied from the source XML and the my namespace from the destination XML. You'll have to substitute the my namespaces in the XSLT with the namespace for your own InfoPath form template.
- Add a Button control to the InfoPath form template.
Add the following C# code in the Clicked event handler of the button:
// Retrieve the xsl to use to transform the SharePoint list
DataSource ds = this.DataSources["transform"];
XslCompiledTransform trans = new XslCompiledTransform();
using (MemoryStream ms = new MemoryStream())
// Transform the SharePoint list and save the XML into the MemoryStream
trans.Transform(DataSources["SimpleList"].CreateNavigator(), null, ms);
// Rewind the MemoryStream
ms.Position = 0;
// Load the MemoryStream into an XPathDocument
XPathDocument doc = new XPathDocument(ms);
// Get a reference to the group1 node
XPathNavigator group1 =
// Replace the group1 node with the transformed XML
Note: The following additional using statements are required for the code to work:
- Save your work, build the project, and test the InfoPath form.
You should now have a fully functional form, so that when you click the button, the data in the SharePoint list is
transformed and copied over to the repeating table in the Main data source of the InfoPath form.
Figure 5. Repeating table in InfoPath displaying transformed SharePoint list data.
Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.
Related InfoPath Articles:
- How to add a data connection to a SharePoint list or library in InfoPath
- How to submit the rows of a repeating table in InfoPath to a SharePoint list
- Programmatically copy rows from a SharePoint list to a repeating node in the Main data source
- SharePoint list CRUD operations using an InfoPath repeating table
- Add a new SharePoint list using an InfoPath form