Removing and Adding child nodes bound to external data source - InfoPath Dev
in

InfoPath Dev

Removing and Adding child nodes bound to external data source

Last post 03-10-2015 11:33 AM by BejeweledOne. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 03-10-2015 11:33 AM

    Removing and Adding child nodes bound to external data source

    So this is my first foray into using code with InfoPath. Actually it's only my second foray into c#, so I came here to the experts for assistance. Here is the scenario based on my customer's requirements. For this particular piece of the form there are 2 external data sources. One is Regions and one is Market (Market contains countries and a lookup to regions).

    There are 2 multi-select list boxes on the form. One contains Regions and the other contains Countries. I have successfully gotten the code to work (with help) to select the correct countries when a region is selected in the regions box. The issue comes in when a country is 'deselected' in the regions box. When a region is de-selected the assiciated countries need to be deselected also. Trying to just use the SetValue and set it to SetValue("") removes the node rather than just removing the value. Any help in knowing how to accomplish this will be very helpful and appreciated.

    using Microsoft.Office.InfoPath;
    using Microsoft.Office.InfoPath;
    using System;
    using System.Xml;
    using System.Xml.XPath;

    namespace multi_select_test
    {
    public partial class FormCode
    {
    public void InternalStartup()
    {
    EventManager.XmlEvents["/my:myFields/my:strRegion"].Changed += new XmlChangedEventHandler(strRegion_Changed);
    }

    public void strRegion_Changed(object sender, XmlEventArgs e)
    {
    XPathNavigator fields = MainDataSource.CreateNavigator();

    //XPathNodeIterator ctrs = fields.CreateNavigator().Select("/my:myFields/my:group2/my:field2", NamespaceManager);
    //foreach (XPathNavigator ctr in ctrs)
    //{
    // if (ctr.InnerXml != "")
    // {
    // ctr.SetValue("");
    // }
    //}

    XPathNodeIterator fr = fields.Select("/my:myFields/my:group1/my:field1", NamespaceManager);

    foreach (XPathNavigator reg in fr)
    {
    string formregion = reg.Value;

    XPathNavigator ds = DataSources["Country"].CreateNavigator();
    XPathNodeIterator Country = ds.Select("/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW", NamespaceManager);

    foreach (XPathNavigator item in Country)
    {
    string region = item.SelectSingleNode("d:Region", NamespaceManager).Value;
    string country = item.SelectSingleNode("d:Title", NamespaceManager).Value;

    if (region == formregion)
    {
    AddItem(country);
    }
    }
    }
    }

    private void AddItem(string itemId)
    {
    XPathNavigator DOM = MainDataSource.CreateNavigator();
    XPathNavigator group2 = DOM.SelectSingleNode("//my:group2", NamespaceManager);
    XPathNavigator field2 = DOM.SelectSingleNode("//my:group2/my:field2", NamespaceManager);
    XPathNavigator newNode = field2.Clone();

    newNode.SetValue(itemId);

    group2.AppendChild(newNode);
    }


    }
    }
Page 1 of 1 (1 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.