Accessing Infopath fields, a simpler way.

Posted on 12/1/2007 @ 2:55 AM in #SharePoint by | Feedback | 6615 views

What is the conventional way you'd access fields in an infopath form in VSTA code?

XPathNavigator myNavigator =
    this.MainDataSource.CreateNavigator().SelectSingleNode( "/my:myFields/my:customerName", NamespaceManager);

The challenge here is that complex, and I'd argue, unmaintainable XPATH that you need to get by right clicking on each property in a given datasource, and choosing "Copy XPath".

Is there a better way?

Umm .. yeah!

Here's what you do.

  • Open the published form in Design mode.
  • Choose File --> Save as source files.
  • Delete everything except myschema.xsd
  • Open the myschema.xsd, and add "nillable=true" on all xsd:elements, you have to do this because MSFT's XML doesn't match W3C's.
  • run xsd.exe myschema.xsd /c to generate a myschema.cs
  • Add the myschema.cs to your VSTA project.
  • Now use strongly typed code as shown below:

XmlSerializer serializer = new XmlSerializer(typeof(myFields));

XmlTextReader rdrMyFields = new XmlTextReader(
    new System.IO.StringReader(
    this.CreateNavigator().SelectSingleNode(
    "/my:myFields", NamespaceManager).OuterXml));
myFields fields = (myFields)serializer.Deserialize(rdrMyFields);
fields.customerName = "Sahil Malik";

Cool huh? :-)

Where this really shines is, if you have binary fields, such as an image.

Sound off but keep it civil:

Older comments..


On 2/29/2008 5:43:53 PM Jason said ..
This is great... I am just trying to get it working in vb though. I also submit my InfoPath form as the "my:myfields" tree + sub nodes. Not sure if this makes a difference...

Somehow I am doing this wrong but:


- "newform" is the object passed (by infopath) as instance of the class created by xsd.exe /c "InfoPathScannerRequestService" is the namespace, "myFields" is the class name. This is what I have:

Dim serializer As New XmlSerializer(GetType(InfoPathScanningRequestService.myFields))


Dim rdrMyFields As New XmlTextReader(New System.IO.StringReader(Me.CreateNavigator.SelectSingleNode("/my:myFields", NamespaceManager).OuterXml))


Dim fields As myFields = DirectCast(serializer.Deserialize(rdrMyFields), myFields)


fields.customerName = "Sahil Malik"

The "Me.CreateNavigator" and "NamespaceManager" are not defined. I read that the .CreateNavigator method is a member of XmlNode but I don't have one because the xml was typed to the class "myFields". Any help?


On 6/10/2008 1:59:48 PM CodeGator said ..
Hello Jason,

Be sure your code imports System.Xml. Also, make sure you are trying to run this code from the form's code-behind. (FormCode). Do those two things and it should compile. Here is my attempt, that I just compiled:

Dim s As New XmlSerializer(Type.GetType("myFields"))


Dim r As New XmlTextReader(New StringReader(Me.CreateNavigator().SelectSingleNode("/my:myFields", NamespaceManager).OuterXml))


Dim m As myFields = s.Deserialize(r)


m.field1 = "100"

I hope that helps,

Martin


On 9/3/2012 4:15:27 PM Nesreen said ..
Kindly I need your help in this , I have a forms library at SP 2007 with infopath form that has a repeating table , what I need is when a form is submited to SP the event handler on the form libary to read the data from the repeating table at the form and fill it in alist at SP.

any help is apperciated

Thanks