none
Infopath code for SharePoint Form

    Question

  • I am relatively new to InfoPath and C#, but i am trying to get a bit of code working.  I am using InfoPath 2013 with SharePoint 2016.  What I am trying to do is use a SOAP web service to populate a dropdown field with Managed MetaData from the SharePoint site.  The SOAP datasource seems to be working fine, but my code for handling the XML return is not... like i said I am new to C# and cannot find the problem, and the error i get "Schema validation found non-data type errors" isn't helping me much...  any help would be appreciated. I am posting my code below... 

    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;
    
    namespace AuditTest_10000
    {
        public partial class FormCode
        {
            public void InternalStartup()
            {
                EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
            }
    
     public void FormEvents_Loading(object sender, LoadingEventArgs e)  
         {  
           XPathNavigator mainNav = this.MainDataSource.CreateNavigator();  
           // Retrieve list of Locations from SharePoint 
           populateDropdown(
             mainNav, "/my:myFields/my:tblLoc/my:Loc", "LocResults",
            "Loc", "LocValue", "LocGuid", "/my:myFields/my:tblLoc"  
           );  
         }  
          private void populateDropdown(  
           XPathNavigator nav, string fieldXPath, string dataSourceName,   
           string fieldName, string valueLabel, string guidLabel, string groupXPath  
           )  
         {  
           cleanupDropdown(nav, fieldXPath);  
           XPathNavigator termSetNav = this.DataSources[dataSourceName].CreateNavigator();  
           string xPath = "/dfs:myFields/dfs:dataFields/tns:GetTermSetsResponse/tns:GetTermSetsResult";  
           XPathNavigator nodeNav = termSetNav.SelectSingleNode(xPath, this.NamespaceManager);  
           string resultXML = nodeNav.Value.ToString();  
           XmlDocument termSetXML = new XmlDocument();  
           termSetXML.LoadXml(resultXML);  
           XmlNodeList terms = termSetXML.GetElementsByTagName("T");  
           foreach (XmlNode term in terms)  
           {  
             XmlNode v = term.SelectSingleNode("LS/TL");  
             string termValue = v.Attributes["a32"].Value;  
             string termGuid = term.Attributes["a9"].Value;  
             if (!string.IsNullOrEmpty(termValue))  
               addDropdown(fieldName, termValue, valueLabel, termGuid, guidLabel, groupXPath, nav);  
           }  
         }  
         private void cleanupDropdown(XPathNavigator nav, string xPath)  
         {  
           XPathNodeIterator rows = nav.Select(xPath, NamespaceManager);  
           for (int i = rows.Count; i > 0; i--)  
           {  
             XPathNavigator delRow = nav.SelectSingleNode(xPath + "[" + i + "]", NamespaceManager);  
             if (delRow != null)  
               delRow.DeleteSelf();  
           }
         }
         private void addDropdown(string groupName, string value, string valueLabel,   
           string guid, string guidLabel, string xPath, XPathNavigator nav)  
         {  
           XmlDocument doc = new XmlDocument();  
           XmlNode group = doc.CreateElement(groupName, NamespaceManager.LookupNamespace("my"));  
           XmlNode field = doc.CreateElement(valueLabel, NamespaceManager.LookupNamespace("my"));  
           XmlNode node = group.AppendChild(field);  
           node.InnerText = value;  
           XmlNode fieldGuid = doc.CreateElement(guidLabel, NamespaceManager.LookupNamespace("my"));  
           XmlNode guidNode = group.AppendChild(fieldGuid);  
           guidNode.InnerText = guid;  
           doc.AppendChild(group);  
           nav.SelectSingleNode(  
            xPath,  
            NamespaceManager) .AppendChild(doc.DocumentElement.CreateNavigator());  
            }    
        }
    
        }

    Tuesday, March 14, 2017 2:30 PM

All replies