Powered By Blogger

Tuesday, January 17, 2012

Read and Bind InfoPath repeating table programmatically

Using methods of XPathNavigator class  repeating table of InfoPath can be bound with dataset / table / data view .  Values available in repeating table can also be accessed using methods of same class.

Line of code to bind and read repeating table in below data source,

 

 

To Bind : -

DataSet ds = new DataSet();

                XPathNavigator xNavigator = this.MainDataSource.CreateNavigator();

                ds = GetDatasetFromAdapter();

 

                XPathNavigator xRepeatingNavigator = xNavigator.SelectSingleNode("/my:myFields/my:Group/my:RepeatingGroup[1]", NamespaceManager);

                XPathNavigator xParentNavigator = xNavigator.SelectSingleNode("/my:myFields/my:Group", NamespaceManager);

 

                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)

                {

                    XPathNavigator node = xRepeatingNavigator.Clone();

                    node.SelectSingleNode("my:FirstValue", NamespaceManager).SetValue(ds.Tables[0].Rows[i]["FirstValue"].ToString());

                    node.SelectSingleNode("my:SecondValue", NamespaceManager).SetValue(ds.Tables[0].Rows[i]["SecondValue"].ToString());

                    xParentNavigator.AppendChild(node);

 

                }

                xRepeatingNavigator.DeleteSelf();

 

To Read : -

XPathNavigator xNavigator = this.MainDataSource.CreateNavigator();

                XPathNodeIterator xNodeIterator = xNavigator.Select("/my:myFields/my:Group/my:RepeatingGroup", NamespaceManager);

                string FirstValue = string.Empty;

                string SecondValue = string.Empty;

                foreach (XPathNavigator Node in xNodeIterator)

                {

                    FirstValue = Node.SelectSingleNode("my:FirstValue", NamespaceManager).Value;

                    SecondValue = Node.SelectSingleNode("my:SecondValue", NamespaceManager).Value;

                }

Saturday, January 14, 2012

Fill in InfoPath dropdown list from SharePoint List programmatically.

This could easily be done by creating a list data connection. However, same could also be done programmatically in browser InfoPath Form.

1. Create a repeating group having two fields for DisplayName and Value. In this example I have created CustomerName and CustomerId. Data Source for this example are,












2. Drag a dropdown control on design surface and bind it with field (Customer) . Right click on it and select “DropDownList Box Properties”. Select “Get Choices from fields in this form”. In entries select XPath of repeating group and set fields for DisplayName and Value.




















3. Create a list in sharepoint site from which you will get the data to fill this dropdown list.


4. Open code editor (VSTA) for this form and write a method “FillDropList” that will fill values in repeating group and same will appear in dropdown list.


XPathNavigator RootNode = this.MainDataSource.CreateNavigator();

try

{

XPathNavigator xNode = RootNode.SelectSingleNode("/my:myFields/my:CustomerDetails/my:Customers[1]", NamespaceManager);

XPathNavigator xParentNode = RootNode.SelectSingleNode("/my:myFields/my:CustomerDetails", NamespaceManager);

SPWeb web = SPContext.Current.Site.OpenWeb();

SPListItemCollection listItems = web.Lists["Customer"].Items;

foreach (SPListItem item in listItems)

{

XPathNavigator node = xNode.Clone();

node.SelectSingleNode("my:DisplayName", NamespaceManager).SetValue(item["CustomerName"].ToString());

node.SelectSingleNode("my:CustomerId", NamespaceManager).SetValue(item["CustomerId"].ToString());

xParentNode.AppendChild(node);


}

xNode.DeleteSelf();


}

catch (Exception ex)

{

//CreateLog(ex.Message);

}


Call this method wherever required.