Programmatically create an InfoPath form from scratch using a Console application and C# code

Use a Console Application and classes in the System.Xml and System.IO namespaces to programmatically generate the XML for the InfoPath form.

ADVERTISEMENTS

Problem

You published an InfoPath form template to a SharePoint form library and would now like to programmatically create an InfoPath form, which is based on this form template, and upload it to SharePoint after you create it.

Solution

Use a Console Application and classes in the System.Xml and System.IO namespaces to programmatically generate the XML for the InfoPath form.

Discussion

You can accomplish this functionality as follows:

  1. Create an InfoPath form template with a Date Picker field.
  2. Publish the form template to a SharePoint form library.
  3. Go to the SharePoint form library to which you published the form template, fill out a new form, and save it in the form library.
  4. Click on the newly created form in the form library, select Send To > Download a Copy from the context menu, and save the form locally on disk.
  5. Go to the location where you saved the form on your local disk and open it in Notepad. As you can see, InfoPath forms are XML files, so if you want to programmatically create an InfoPath form, you have to write code to programmatically generate the XML of the InfoPath form, which looks similar to the XML of the InfoPath form you downloaded, and replace the values of fields in the form with the desired new values. An important thing you need to make sure of is that the href attribute in the mso-infoPathSolution processing instruction points to the correct form template you published.
  6. Open Microsoft Visual Studio 2005 and create a new Console Application project.
  7. Import the following namespaces:
    using System.Xml;
    using System.IO;
    using System.Net;
  8. Put the following C# code in the Main method of the Console Application:
    byte[] infoPathFormData = null;

    using (MemoryStream ms = new MemoryStream())
    {
    using (XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8))
    {
    // Define the namespace for the form
    string myNamespace = "http://schemas.microsoft.com/office/infopath/2003/myXSD/2007-09-02T01:11:44";

    writer.WriteStartDocument();

    // Create the required processing instructions
    writer.WriteProcessingInstruction(
    "mso-infoPathSolution",
    "name=\"urn:schemas-microsoft-com:office:infopath:ProgCreateForm:-myXSD-2007-09-02T01-11-44\" solutionVersion=\"1.0.0.2\" productVersion=\"12.0.0.0\" PIVersion=\"1.0.0.0\" href=\"http://servername/formlibname/forms/template.xsn\"");
    writer.WriteProcessingInstruction(
    "mso-application",
    "progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.2\"");

    // Create the XML of the main data source of the form
    writer.WriteStartElement("my", "myFields", myNamespace);
    writer.WriteStartElement("my", "field1", myNamespace);
    // Fill the date field with today's date
    writer.WriteString(DateTime.Now.ToString("yyyy-MM-dd"));
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();
    }

    infoPathFormData = ms.GetBuffer();
    ms.Close();
    }

    // Upload the newly created InfoPath form to SharePoint
    if (infoPathFormData != null && infoPathFormData.Length != 0)
    {
    using (WebClient client = new WebClient())
    {
    // Set the credentials to be used for upload to SharePoint
    client.Credentials = CredentialCache.DefaultCredentials;

    // Upload the newly created form to a SharePoint form library
    client.UploadData(
    @"http://servername/formlibname/newformname.xml",
    "PUT",
    infoPathFormData);
    }
    }

    Replace the code that generates the XML in the code snippet above with code to generate the XML for your own InfoPath form. The code displayed in this article will not work for your InfoPath form, but is specific to the InfoPath form and form template that were used when this article was written.

You should now be able to programmatically create the XML for an InfoPath form from scratch and upload it to a SharePoint form library.

 
 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