Showing posts with label infopath form. Show all posts
Showing posts with label infopath form. Show all posts

Friday, 13 April 2012

Create function in infopath form from sharepoint list value

First, you need to set an onChange event on your 2 dropdown boxes. To do so, right click your dropdown boxes --> programming --> change event. This will create a visual studio project with an empty function for your dropdown box. In this function, write this code (or something similar) :

XPathNavigator root = MainDataSource.CreateNavigator();
string FirstName = root.SelectSingleNode("<XPath of your FirstName dropdown box>", NamespaceManager).Value;
string LastName = root.SelectSingleNode("<XPath of your LastName drop down box>", NamespaceManager).Value;


SPSite oSiteCollection = newSPSite(<URL of your server, eg http://portal/>);
SPWeb oWebsiteSrc = oSiteCollection.AllWebs["<URL of the site containing the sharepoint list, eg /sites/finance/expenseaccount>"];
SPList oList = oWebsiteSrc.Lists["<Name of your list>"];

string camlQuery = "<Where><And><Contains><FieldRef Name='Name of the FirstName column'/><Value Type='Text'>" + FirstName + "</Value></Contains><Eq><FieldRef Name='Name of the LastName column' /><Value Type='Text'>" + LastName + "</Value></Eq></And></Where>";
SPQuery query = newSPQuery();
query.Query = camlQuery;


SPListItemCollection collListItemsSrc = oList.GetItems(query);

root.SelectSingleNode("<XPath of your Age Field>", NamespaceManager).SetValue(collListItemsSrc[0].GetFormattedValue("<Name of the Age column>"));