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.
Thanks Abhishek,very good article.
ReplyDelete