Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

InfoPath 2007 Helper for MOSS 2007

5.00/5 (1 vote)
6 Feb 2009GPL31 min read 28.2K   151  
This article is about how to get or set values of an InfoPath form.

Introduction

Recently, I needed to interact with InfoPath 2007 Forms from the Forms Library in MOSS 2007. So, looking around the web and after compiling some information, I made a Helper to serialize and deserialize the components of an InfoPath Form from/to objects in C#. The idea is that using a Workflow, I can capture or set the value of any component of the form.

Background

We are going to use two methods for manipulate the InfoPath's control. First, using the classic XML with the XPathNavigator and the XmlDocument object model. We can get and set values with this method. The other will be using the class exported from the InfoPath Form code using XSD.exe. For the first method, I took the idea from: http://www.bizsupportonline.net/infopath2007/programmatically-retrieve-infopath-form-from-sharepoint.htm[^]. For the other method, I used: http://msdn.microsoft.com/en-us/library/bb251017.aspx[^].

Partial Codes

We can use this Helper without MOSS 2007, just like a simple form:

C#
string sStatus = string.Empty;
string sFileSource = string.Empty;
sFileSource = "InfoPathForm.xml";

// Get the value of the field Status from the XML
// representing the InfoPath form
InfoPathHelper.FileName = sFileSource;
sStatus = InfoPathHelper.Value("Status");

// Set the value of the field Status
InfoPathHelper.SetValue("Status", "En Progreso ...");

// We can also get the value of any field by deserializating 
// the InfoPath form with the method DeserializeFile from our Helper
misCampos InfoPathForm = 
  (misCampos)InfoPathHelper.DeserializeFile(sFileSource, typeof(misCampos));
sStatus = InfoPathForm.Status;

Or we can use this Helper with MOSS 2007 in conjunction with Workflows:

C#
// Go to the specific item (the InfoPath form) in our MOSS
SPSite site = new SPSite("http://miportal.intranet");
SPWeb web = site.AllWebs["Sistemas/Incidentes"];
SPList list = web.Lists["FomulariosInfoPath"];
SPListItem item = list.Items[0];

// Set the filename representing the InfoPath form
InfoPathHelper.SPFileName = item;

// Get the value for the field Status from the form
sStatus = InfoPathHelper.Value("Status");

// Set a value to a field of the form
InfoPathHelper.SetValue("Status", "En Progreso ...");

// Another way to get the value of a field in the InfoPath
// form with deserailization. This only allows getting values
// but no setting any value.
// The object InfoPathFromMoss will have all the fields of the form
misCampos InfoPathFormMoss = 
  (misCampos)InfoPathHelper.DeserializeFile(item.File, typeof(misCampos));
sStatus = InfoPathFormMoss.Status;

The Project

The solution was made with Visual Studio 2008 in a host running Microsoft Office SharePoint Server 2007. Also is included the InfoPath form. Happing coding!

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)