2 Ways to loop through InfoPath forms in a SharePoint Form Library

Learn how you can write C# code that uses the SPList, SPListItem, SPFolder, and SPFile classes of the SharePoint Object Model to loop through and retrieve all of the InfoPath forms that are stored in a SharePoint Form Library.

ADVERTISEMENTS

If you use the SharePoint Object Model to access InfoPath forms in SharePoint, you have the option to either use the SPList or the SPFolder class to retrieve and loop through InfoPath forms that are stored within a SharePoint Form Library.

While the SPFolder class offers more direct access to the physical XML file of an InfoPath form stored in a SharePoint Form Library, you can use the SPList class to retrieve other useful information about the form and its metadata, such as for example, the InfoPath form template (XSN) that a form is based on (accessible through the ContentType property of the list item).

Use the SPList class to loop through InfoPath forms

The following code sample makes use of the SPList, SPListItem, and SPFile classes to loop through InfoPath forms, which are stored within a SharePoint Form Library:

using (SPSite site = new SPSite("http://<SharePoint_Site_URL>"))
{
  using (SPWeb web = site.OpenWeb())
  {
    SPList formsLib = web.Lists["FormsLib"];
    if (formsLib != null)
    {
      foreach (SPListItem item in formsLib.Items)
      {
        SPFile ipForm = item.File;
        string fileName = ipForm.Name;
      }
    }
    web.Close();
  }
  site.Close();
}

where FormsLib is the name of a SharePoint Form Library on the site.

Use the SPFolder class to loop through InfoPath forms

The following code sample makes use of the SPFolder and SPFile classes to loop through InfoPath forms, which are stored within a SharePoint Form Library:

using (SPSite site = new SPSite("http://<SharePoint_Site_URL>"))
{
  using (SPWeb web = site.OpenWeb())
  {
    SPFolder formsLib = web.Folders["FormsLib"];
    if (formsLib != null)
    {
      foreach (SPFile ipForm in formsLib.Files)
      {
        string fileName = ipForm.Name;
      }
    }
    web.Close();
  }
  site.Close();
}

where FormsLib is the name of a SharePoint Form Library on the site.

 


Related Posts

 

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. 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.

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