Click here to Skip to main content
13,593,048 members
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 12 Feb 2009
Licenced CPOL

Programmatically create a browser enabled InfoPath form

, 12 Feb 2009
Rate this:
Please Sign up or sign in to vote.
How to programmatically create a browser enabled InfoPath form.


The article gives an overview of the steps and code required to create, fill, and upload an InfoPath form using code.

Design Infopath Form

Create an InfoPath form using the Microsoft InfoPath designer. This sample form will store the name and surname of the client and a table of products and amounts.


Create Class Representation of the Infopath Form

  1. Save the InfoPath form:
  2. Image2.jpg

    Figure 2 - Save as Source File
    </li />
  3. Load the Visual Studio Command Prompt:
  4. Image3.jpg

  5. Locate form the source files:
  6. Image4.jpg

  7. Execute the command xsd.exe /c /l:CS myschema.xsd.
  8. The XSD tool forms part of the .NET framework tools. We will use it to generate a common language class from the XSD file representation of the InfoPath form.


    Figure 5 – Generated myschema class
// This source code was auto-generated by xsd, Version=2.0.50727.1432.
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
public partial class myFields {
private string nameField;
private string surnameField;
private ProductRow[] productTableField;
private System.Xml.XmlAttribute[] anyAttrField;.....

Once the file is available in the project, you have the capability to bind the data source to the .NET class and access the data in a standardized format. If you ever make changes to your InfoPath data source, you’ll need to rerun the xsd.exe command against the new “schema.xsd” file.

Upload Form Template to a SharePoint Form Site


Figure 6 – Publish


Figure 7


Figure 8 – Select SharePoint Server


Figure 9


Figure 10


Figure 11


Figure 12


Figure 13

Populate and Upload Form Data

Populate the myFields class with dummy data. The class was created using the XSD application.

// Populate myfields class with dummy data. The class was created using the xsd
// application.
myFields fields = new myFields();
fields.Name = "Joe";
fields.Surname = "Blogs";
ProductRow[] rows = new ProductRow[2];
rows[0] = new ProductRow();
rows[0].ProductAmount = "1";
rows[0].ProductName = "Peach Jam";
rows[1] = new ProductRow();
rows[1].ProductAmount = "2";
rows[1].ProductName = "Strawberry Jam";
fields.ProductTable = rows;
// Create infopath form
MemoryStream myInStream = new MemoryStream();
// Form site used to store the generated forms
string rFormSite = @"http://sp/Sample Form";
// Location where form template is stored.
string rTemplateLocation = @"http://sp/Sample Form/Forms/template.xsn"; 
using (myInStream)
    // InfoPath forms are XML files. We have to write code to programmatically
    // generate the XML of the InfoPath form
    //Create XmlSerializer using myfields type
    XmlSerializer serializer = new XmlSerializer(typeof(myFields));
    XmlTextWriter writer = new XmlTextWriter(myInStream, Encoding.UTF8);
    // Insert infopath processing instructions
    string rInstruction = 
      "name=\"urn:schemas-microsoft-com:office:infopath:" + 
    rInstruction += 
        "solutionVersion=\"\" productVersion=\"\"
        PIVersion=\"\" href=\"" + rTemplateLocation + "\"";
    // 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. 
    writer.WriteProcessingInstruction("mso-infoPathSolution", rInstruction);
    // Mark the xml as an infopath document. 
        "progid=\"InfoPath.Document\" versionProgid=\"InfoPath.Document.2\"");
    // Serialize infopath data
    serializer.Serialize(writer, fields);
    // Upload form to form site.
    using (SPSite site = new SPSite(rFormSite))
        // Create web instance.
        using (SPWeb spweb = site.OpenWeb())
            // Add the file to the web file list.
            spweb.Files.Add(rFormSite + "\\" + Path.GetRandomFileName() + ".xml",



Figure 14 – New Item in Form Library


Figure 15 – Form Loaded with Data


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Simon Bonello
Malta Malta
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionRepecting table Pin
Vikram Stephen8-Apr-13 17:14
memberVikram Stephen8-Apr-13 17:14 
GeneralMy vote of 5 Pin
amirhosseinhashemzadeh11-Sep-11 23:31
memberamirhosseinhashemzadeh11-Sep-11 23:31 
QuestionSSIS data flow compoenent Pin
kuthuparakkal2-Aug-11 15:29
memberkuthuparakkal2-Aug-11 15:29 
AnswerRe: SSIS data flow compoenent Pin
DeadLock062-Nov-11 5:11
memberDeadLock062-Nov-11 5:11 
GeneralMy vote of 4 Pin
Member 43146617-Jul-10 4:17
memberMember 43146617-Jul-10 4:17 
GeneralCheckboxes and Date Fields Pin
Jennifer Berg11-Mar-10 4:35
memberJennifer Berg11-Mar-10 4:35 
AnswerRe: Checkboxes and Date Fields Pin
Jennifer Berg11-Mar-10 7:08
memberJennifer Berg11-Mar-10 7:08 
GeneralRe: Checkboxes and Date Fields Pin
Scott T.31-Aug-12 6:12
memberScott T.31-Aug-12 6:12 
QuestionGreate tips! Pin
ErwinGan17-Jan-10 6:28
memberErwinGan17-Jan-10 6:28 
Generalfieldxxxspecified in browser form template prevents fieldxxx from submitting to form library Pin
markah_199918-Dec-09 4:38
membermarkah_199918-Dec-09 4:38 
QuestionRe: fieldxxxspecified in browser form template prevents fieldxxx from submitting to form library Pin
Member 43146617-Jul-10 4:16
memberMember 43146617-Jul-10 4:16 
QuestionQuestion regarding the generated Xml file Pin
Zaid A.26-Mar-09 7:28
memberZaid A.26-Mar-09 7:28 
QuestionRe: Question regarding the generated Xml file Pin
Hamez16-Jun-09 21:31
memberHamez16-Jun-09 21:31 
AnswerRe: Question regarding the generated Xml file Pin
Hepple16-Jul-09 1:48
memberHepple16-Jul-09 1:48 
GeneralRe: Question regarding the generated Xml file Pin
Kalyan_Vizag21-Jul-09 2:12
memberKalyan_Vizag21-Jul-09 2:12 
QuestionRe: Question regarding the generated Xml file Pin
SharePointLearner8-Apr-13 9:58
memberSharePointLearner8-Apr-13 9:58 
QuestionRe: Question regarding the generated Xml file Pin
SharePointLearner8-Apr-13 8:17
memberSharePointLearner8-Apr-13 8:17 
QuestionNice work a few corrections I made... [modified] Pin
Peter Weissbrod17-Mar-09 11:08
memberPeter Weissbrod17-Mar-09 11:08 
AnswerRe: Nice work a few corrections I made... Pin
Simon Bonello20-Mar-09 3:06
memberSimon Bonello20-Mar-09 3:06 
AnswerRe: Nice work a few corrections I made... Pin
markah_199918-Dec-09 4:37
membermarkah_199918-Dec-09 4:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180618.1 | Last Updated 12 Feb 2009
Article Copyright 2009 by Simon Bonello
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid