Create a custom SharePoint list from a repeating table in an InfoPath Programmatically

Here I am explaining custom SharePoint list from a repeating table in an InfoPath Programmatically. If 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.

Steps are as follows...

1) Open InfoPath from Microsoft Office InfoPath

2) Create a new Blank browser-compatible web form template.

3) Add a Text Box control, a Table control with at least one column, and a Button control to the InfoPath form template.

4) Name the Text Box control "listName" and the field in the Table "fieldName".

5) Open button control Properties dialog box.

6) On the Button Properties dialog box, click Edit Form Code, and add the following code to the Button Click event handler of the button:

XPathNavigator root = MainDataSource.CreateNavigator();
string listName = root.SelectSingleNode(  "//my:listName", NamespaceManager).Value;
XPathNodeIterator iter = root.Select(  "//my:fieldName", NamespaceManager);
SPWeb web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
// Create a custom list
Guid listGuid = web.Lists.Add(  listName,  "This custom list was created by InfoPath",
  SPListTemplateType.GenericList);
// 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

string query = list.Views["All Items"].Query;
SPViewCollection viewcoll = list.Views;
Guid viewID = list.Views["All Items"].ID;
viewcoll.Delete(viewID);
// Add 2 standard SharePoint fields to a new view
StringCollection viewfields = new StringCollection();
viewfields.Add("Attachments");
viewfields.Add("LinkTitle");
// Create the list columns
while (iter.MoveNext())
{
  string fieldName = iter.Current.Value;
  list.Fields.Add(fieldName, SPFieldType.Text, false);
  // Add the field
  viewfields.Add(fieldName);
}
// Add the view to the list
list.Views.Add("All Items", viewfields, query, 100, true, true);
// Save the changes
list.Update();
web.AllowUnsafeUpdates = false;

7) Add a reference to the Microsoft.SharePoint DLL to the project.

8) Add following namespaces
       using System.Collections.Specialized;
       using Microsoft.SharePoint;
9) Build project.

10) Give the InfoPath form template

11 ) Publish the InfoPath form template to a SharePoint server running InfoPath Forms Services.

12) Give appropriate permissions to InfoPath form
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now