Hi,
I'm not 100% sure where this should be filed, but I'm hoping someone can help me out here. I have a secondary Datasource, which performs a lookup to a sharepoint list to pull down info. This datasource is used to populate a dropdown box for a list. What I was attempting to do, was after the user selects a value for the dropdown box, use that to populate a repeating table with all entries that are valid for that entity. I created a changed event in my code and did the following (which is essentially similar to these - (http://www.infopathdev.com/forums/p/9873/90584.aspx and http://www.bizsupportonline.net/infopath2007/copy-rows-from-sharepoint-list-to-main-data-source.htm)
my code looks like the following:
namespace NewMatterRequest
{
public partial class FormCode
{
// Member variables are not supported in browser-enabled forms.
// Instead, write and read these values from the FormState
// dictionary using code such as the following:
//
// private object _memberVariable
// {
// get
// {
// return FormState["_memberVariable"];
// }
// set
// {
// FormState["_memberVariable"] = value;
// }
// }
// NOTE: The following procedure is required by Microsoft InfoPath.
// It can be modified using Microsoft InfoPath.
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 static string GetValueOrEmptyString(XPathNavigator node)
{
return node == null ? string.Empty : node.Value;
}
private void AddItem(string suretyname, string clientno, string status, string relationship)
{
XPathNavigator mainDS = MainDataSource.CreateNavigator();
XPathNavigator group1 = mainDS.SelectSingleNode(
"/my:myFields/my:group1", NamespaceManager);
group1.AppendChildElement("my", "group2", NamespaceManager.LookupNamespace("my"), "");
XPathNodeIterator iter = mainDS.Select("/my:myFields/my:group1/my:group2", NamespaceManager);
int rowCount = 0;
if (iter != null)
rowCount = iter.Count;
XPathNavigator lastGroup2 = mainDS.SelectSingleNode(
"/my:myFields/my:group1/my:group2[" + rowCount + "]", NamespaceManager);
if (lastGroup2 != null)
{
lastGroup2.AppendChildElement("my", "field1", NamespaceManager.LookupNamespace("my"), suretyname);
lastGroup2.AppendChildElement("my", "field2", NamespaceManager.LookupNamespace("my"), clientno);
lastGroup2.AppendChildElement("my", "field3", NamespaceManager.LookupNamespace("my"), status);
lastGroup2.AppendChildElement("my", "field4", NamespaceManager.LookupNamespace("my"), relationship);
}
}
public void InternalStartup()
{
EventManager.XmlEvents["/my:myFields/my:fielddb"].Changed += new XmlChangedEventHandler(field4_Changed);
}
public void field4_Changed(object sender, XmlEventArgs e)
{
// Write your code here to change the main data source.
//retrieve value of dropdown box for comparison purposes
string value = MainDataSource.CreateNavigator().SelectSingleNode(
"/my:myFields/my:fielddb", NamespaceManager).Value;
XPathNavigator secDSNav = DataSources["Surety"].CreateNavigator();
// Retrieve the rows of the secondary data source
XPathNodeIterator rows = secDSNav.Select(
"/dfs:myFields/dfs:dataFields/ns1:SharePointListItem_RW", NamespaceManager);
// Loop through the rows of the secondary data source and fill the repeating table
while (rows.MoveNext())
{
string suretyname = GetValueOrEmptyString(rows.Current.SelectSingleNode(
"/dfs:myFields/dfs:dataFields/ns1:SharePointListItem_RW/ns1:Surety_x0020_Insurer_x0020_Name", NamespaceManager));
if (suretyname == value)
{
string clientno = GetValueOrEmptyString(rows.Current.SelectSingleNode(
"/dfs:myFields/dfs:dataFields/ns1:SharePointListItem_RW/ns1:Title", NamespaceManager));
string status = GetValueOrEmptyString(rows.Current.SelectSingleNode(
"/dfs:myFields/dfs:dataFields/ns1:SharePointListItem_RW/ns1:Matter_x0020_Open_x0020_or_x0020", NamespaceManager));
string relationship = GetValueOrEmptyString(rows.Current.SelectSingleNode(
"/dfs:myFields/dfs:dataFields/ns1:SharePointListItem_RW/ns1:Adverse_x002f_Represent", NamespaceManager));
// Add the item to the repeating table
AddItem(suretyname, clientno, status, relationship);
}
}
// Remove the first empty item from the repeating table
DeleteFirstEmptyItem();
}
}
}
When I run this, I have two issues: 1). It only works if I select the first item in the dropdown box and 2). When it works using the first value of the dropdown box, instead of only performing the AddItem() on the items that have the same name as that value, it does it for all 400+ items in that list. Running through debug, I see that the value called here: "string suretyname = GetValueOrEmptyString(rows.Current.SelectSingleNode(
"/dfs:myFields/dfs:dataFields/ns1:SharePointListItem_RW/ns1:Surety_x0020_Insurer_x0020_Name", NamespaceManager));"
never changes throughout the life of the loop, and is never equal to any other value other than the first value of the dropdown box, so seemingly those selectsinglenode calls only ever see the first result.
I guess my questions are:
1. how do I make the loop iterate through each result from the rows and do this? I was under the impression that the rows.moveNext() would be essentially a for-each loop, but that doesn't seem to be the case.
2. In a couple of the examples I linked, I note that when calling the rows.Current.SelectSingleNode() operation, the namespace is just "@", i.e. "@First_Name" in one of them. If I try that infopath raises an exception. What does the "@ do in the examples posted? I used the Copy Xpath function from my secondary datasource in order to fill in the location on my rows.current.selectsinglenode() calls.
3. Am I doing this in a totally stupid way? Is there a way to filter this datasource programmatically? In the infopathdev forum post I linked above Hilary Stoupa suggests a method, but I can't seem to get it to work. Of course, that could be all related to the issues I'm having already?
Does anyone have any help or insight available into what I am doing wrong here? I've been staring at this for several hours and am not making any headway >.