15 August 2012

How do I - Programmatically select a field inside InfoPath XML using C#

As we know, InfoPath forms are actually XML documents behind the scenes... a special kind of XML, but XML nonetheless. As such we should be able to figure out how to change the value of a field pretty easily, right? You'd think so, but unless you live inside XML every day (so few of us do these days) it might now be as easy as you'd have thought. After getting the InfoPath document loaded to an XmlDocument object, one would expect a statement thus:
doc.SelectSingleNode("//my:Family", nsm).InnerText = "New Value";

to have the desired effect. Alas, it's not that simple. It never is, is it? Smile Instead, this is what you're greeted with:
image
What we need to do is ensure that your XmlNamespaceManager is properly assigned with the content from the given InfoPath form in order to translate the "my:" part of our xPath statement. If we take a look at the InfoPath document's OuterXml value we see this:
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:pc="http://schemas.microsoft.com/office/infopath/2007/PartnerControls" 
             xmlns:ma="http://schemas.microsoft.com/office/2009/metadata/properties/metaAttributes" 
             xmlns:d="http://schemas.microsoft.com/office/infopath/2009/WSSList/dataFields" 
             xmlns:q="http://schemas.microsoft.com/office/infopath/2009/WSSList/queryFields" 
             xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" 
             xmlns:dms="http://schemas.microsoft.com/office/2009/documentManagement/types" 
             xmlns:tns="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" 
             xmlns:s1="http://microsoft.com/wsdl/types/" 
             xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/directory/" 
             xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
             xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
             xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
             xmlns:ns2="http://schemas.xmlsoap.org/wsdl/soap/" 
             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
             xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
             xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" 
             xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
             xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-02-03T15:25:24"
             xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
  <my:Family>
    <my:InspectionDate>
      2012-03-16T00:00:00Z
    </my:InspectionDate>

With so many xmlns attributes in the InfoPath document, we need some automation to prepare our XmlNamespaceManager so we can interact with the data properly. Adding this little code snippet to our existing code, should resolve that nasty error:
foreach (XmlAttribute att in doc.DocumentElement.Attributes)
{
  if (att.Prefix == "xmlns")
  {
    nsm.AddNamespace(att.LocalName, att.Value);
  }
}

Remember to create the XmlNamespaceManager object with a reference to the base XmlDocument's NameTable thus:
nsm = new XmlNamespaceManager(doc.NameTable);
Throw all that together and we are now able to manipulate the InfoPath field programmatically to our heart's content. Smile
Later
C

RSS Google+TwitterLinkedInFacebookMVPQuixCC

No comments:

Post a Comment

Comments are moderated only for the purpose of keeping pesky spammers at bay.

SharePoint Remote Event Receivers are DEAD!!!

 Well, the time has finally come.  It was evident when Microsoft started pushing everyone to WebHooks, but this FAQ and related announcement...