![]() |
Enterprise Systems »
SharePoint Server »
General
Beginner
License: The Code Project Open License (CPOL)
Programmatically create a browser enabled InfoPath formBy Simon BonelloHow to programmatically create a browser enabled InfoPath form. |
C#, Windows, .NET, Office, Visual-Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
The article gives an overview of the steps and code required to create, fill, and upload an InfoPath form using code.
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.




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.

//
// This source code was auto-generated by xsd, Version=2.0.50727.1432.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,
Namespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-02-04T09:20:42")]
[System.Xml.Serialization.XmlRootAttribute(
Namespace="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-02-04T09:20:42",
IsNullable=false)]
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.








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:" +
"UploadSample:-myXSD-2009-02-04T09-20-42\"";
rInstruction +=
"solutionVersion=\"1.0.0.2\" productVersion=\"12.0.0.0\"
PIVersion=\"1.0.0.0\" 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.
writer.WriteProcessingInstruction("mso-application",
"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",
myInStream.GetBuffer());
}
}
}


| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 12 Feb 2009 Editor: Smitha Vijayan |
Copyright 2009 by Simon Bonello Everything else Copyright © CodeProject, 1999-2010 Web18 | Advertise on the Code Project |