0
votes

I'm looking to copy data from a sharepoint connection to a local repeating table so that I can modify and re-submit the data.

I've been looking the article below which seems to describe what I require: http://www.bizsupportonline.net/infopath2007/copy-rows-from-sharepoint-list-to-main-data-source.htm

I'm not familiar with C# so I would like some help on the code as the upper table should match the lower table in the the preview

Thanks in advance

Infopath form screenshot

Secondary connection screenshot

Preview of form after clicking button

C# code linked to button private void AddItem(string title, string id) { XmlDocument doc = new XmlDocument(); XmlNode group = doc.CreateElement("group2", NamespaceManager.LookupNamespace("my"));

        XmlNode field = doc.CreateElement("field1",
        NamespaceManager.LookupNamespace("my"));
        XmlNode node = group.AppendChild(field);
        node.InnerText = title;

        field = doc.CreateElement("field2",
        NamespaceManager.LookupNamespace("my"));
        node = group.AppendChild(field);
        node.InnerText = id;

        doc.AppendChild(group);

        MainDataSource.CreateNavigator().SelectSingleNode(
        "/my:myFields/my:group1",
        NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());
    }

    private void DeleteFirstEmptyItem()
    {
        XPathNavigator domNav = MainDataSource.CreateNavigator();
        XPathNavigator itemNav = domNav.SelectSingleNode(
        "/my:myFields/my:group1/my:group2[1]",
        NamespaceManager);

        if (itemNav != null)
            itemNav.DeleteSelf();
    } 

    public void CTRL13_5_Clicked(object sender, ClickedEventArgs e)
    {
        XPathNavigator secDSNav = DataSources["TestCustomList"].CreateNavigator();

        // Retrieve the rows of the secondary data source
        XPathNodeIterator rows = secDSNav.Select(
        "/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW",
        NamespaceManager);

        // Loop through the rows of the secondary data source and fill the repeating table
        while (rows.MoveNext())
        {
            string title = rows.Current.SelectSingleNode(
            "/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW/d:Title", NamespaceManager).Value;
            string id = rows.Current.SelectSingleNode(
            "/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW/d:ID", NamespaceManager).Value;

            // Add the item to the repeating table
            AddItem(title, id );
        }

        // Remove the first empty item from the repeating table
        DeleteFirstEmptyItem();
    }
1

1 Answers

0
votes

After some trial and error, it looks like the declaration of string variables title and id shouldn't refer to the full Xpath

while (rows.MoveNext())
{
    string title = rows.Current.SelectSingleNode(
    "d:Title", NamespaceManager).Value;
    string id = rows.Current.SelectSingleNode(
    "d:ID", NamespaceManager).Value;

     // Add the item to the repeating table
     AddItem(title, id);
}

Desired result