Programmatically create a custom SharePoint list from a repeating table in an InfoPath web-based form
Learn how you can use data from a repeating table in an InfoPath web-based form that you open on a SharePoint site to create a custom SharePoint list.
You have an InfoPath repeating table on a browser form in which you want to enter the names of the columns of a SharePoint list and then click a button to create a custom SharePoint list on the SharePoint site where you opened the InfoPath form.
Write code that uses the SharePoint Object Model in an InfoPath form template to programmatically create a custom SharePoint list on a SharePoint site.
You can achieve this functionality as follows:
- In InfoPath, create a new Blank browser-compatible form template.
- Add a Text Box control, a Repeating Table control with one column, and a Button control to the InfoPath form template.
- Name the Text Box control listName and the field in the Repeating Table fieldName.
- Double-click the button control to open its Properties dialog box.
On the Button Properties dialog box, click Edit Form Code, and add the following code to the Clicked event handler of the button:
XPathNavigator root = MainDataSource.CreateNavigator();
string listName = root.SelectSingleNode(
XPathNodeIterator iter = root.Select(
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
// Create a custom list
Guid listGuid = web.Lists.Add(
"This custom list was created by InfoPath",
// Get the list and set its properties
SPList list = web.Lists[listGuid];
list.OnQuickLaunch = true;
// Copy settings from the All Items view and then delete
// the view
string query = list.Views["All Items"].Query;
SPViewCollection viewcoll = list.Views;
Guid viewID = list.Views["All Items"].ID;
// Add 2 standard SharePoint fields to a new view
StringCollection viewfields = new StringCollection();
// Create the list columns
string fieldName = iter.Current.Value;
list.Fields.Add(fieldName, SPFieldType.Text, false);
// Add the field to the view
// Add the view to the list
list.Views.Add("All Items", viewfields, query, 100, true, true);
// Save the changes
web.AllowUnsafeUpdates = false;
- Add a reference to the Microsoft.SharePoint DLL to the project.
Add the following using statements to the FormCode.cs file:
- Save your work and build the project.
- Give the InfoPath form template full trust.
- Publish the InfoPath form template to a SharePoint server running InfoPath Forms Services. You will have to perform an administrator-approved deployment.
- Give SharePoint users who will use the InfoPath form appropriate permissions to be able to create SharePoint lists on the SharePoint sites to which you've deployed the InfoPath form template.
You should now have a fully functional form, so that when you fill out the repeating table and a name for the list, and then click the button, a custom SharePoint list is created on the SharePoint site where you opened the InfoPath form.
You could extend this solution to include functionality to choose SharePoint field types from a drop-down list box in the repeating table and write code to assign those field types to the columns of the custom SharePoint list.
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:
- 5 Ways to submit an InfoPath form to a SharePoint list
- How to update a SharePoint list via an InfoPath browser form using the SharePoint Object Model
- Add a new SharePoint list using an InfoPath form
- SharePoint list CRUD operations using an InfoPath repeating table
- SharePoint Designer 2013 workflow to loop through InfoPath repeating table