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.

ADVERTISEMENTS

Problem

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.

Solution

Write code that uses the SharePoint Object Model in an InfoPath form template to programmatically create a custom SharePoint list on a SharePoint site.

Discussion

You can achieve this functionality as follows:

  1. In InfoPath, create a new Blank browser-compatible form template.
  2. Add a Text Box control, a Repeating Table control with one column, and a Button control to the InfoPath form template.
  3. Name the Text Box control listName and the field in the Repeating Table fieldName.
  4. Double-click the button control to open its Properties dialog box.
  5. 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(
      "//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
    // the view
    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 to the view
      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;

  6. Add a reference to the Microsoft.SharePoint DLL to the project.
  7. Add the following using statements to the FormCode.cs file:

    using System.Collections.Specialized;
    using Microsoft.SharePoint;

  8. Save your work and build the project.
  9. Give the InfoPath form template full trust.
  10. Publish the InfoPath form template to a SharePoint server running InfoPath Forms Services. You will have to perform an administrator-approved deployment.
  11. 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.

 
 Subscribe for updates via RSS or Email

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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners
InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013
InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners
InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010
InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers
InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services


Related InfoPath Articles:

ADVERTISEMENTS

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services