Saturday, May 19, 2012

Populating InfoPath Drop Down List Box with SharePoint List Lookup Data

Populate InfoPath drop-down programmatically from Sharepoint List Lookup data



  • Article 1: How to code C-Sharp on infopath form 2010
  • Article 2: How to set the value of a Field in Infopath
  • Article 3: How to set value of a field from sharepoint list
  • Article 4: Populating InfoPath Drop Down List Box with SharePoint List Lookup Data 
  • Article 5Populating InfoPath Drop Down List Box with SharePoint Fields Choice
  • Article 6: Deploy an InfoPath 2010 Form with Managed Code to a Browser Enabled Sharepoint Document Library

Download infopath project at here

Designing  and coding infopath

Open Infopath | Design user interface follows as
Go to File => Form Options => Programming
Form template code language: C#
Go to File => Form Options => Security and Trust
Configure follows as:
Go to Tab Developer | click to Loading Event ribbon
Right click to myFields | Add
Name: Countries
Type: Group
Check to Repeating
Right click to Countries | Add
Name: Displayname
Continue Right click to Countries | Add then input Name: Value
Right click to DropdownList “Country” | Drop-Down List Box Properties…
At List box choices------------------------------
Check to radio button “Get choice from fields in this from”, then click to Entries follows as
Select value follows as
Open your  site collection | Create new list Countries then add new some Item
Add reference to Microsoft.sharepoint.dll
Using  using Microsoft.SharePoint;
Add method AddCountries(); to FormEvents_Loading

Paste this method  to below FormEvents_Loading method public void AddCountries()
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;

namespace PopulateDropdownListFromCode
{
    public partial class FormCode
    {       
        // NOTE: The following procedure is required by Microsoft InfoPath.
        // It can be modified using Microsoft InfoPath.
        public void InternalStartup()
        {
            EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            //Populate country drop down
            AddCountries();
        }

        public void AddCountries()
        {
            try
            {
                SPSite site = new SPSite("http://win-67038mbkel7");
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["Countries"];
                SPListItemCollection listitems = list.Items;
                XPathNavigator nav = this.CreateNavigator().
                    SelectSingleNode("/my:myFields/my:Countries", this.NamespaceManager);// Note: "/my:myFields/my:Countries" => Group Countries
                foreach (SPListItem li in listitems)
                {
                    XPathNavigator newNode = null;
                    newNode = nav.Clone();
                    newNode.SelectSingleNode("/my:myFields/my:Countries/my:Displayname",
                        this.NamespaceManager).SetValue(li["Title"].ToString());
                    newNode.SelectSingleNode("/my:myFields/my:Countries/my:Value",
                        this.NamespaceManager).SetValue(li["Title"].ToString());
                    nav.InsertAfter(newNode);
                    newNode = null;
                }
                nav.DeleteSelf();
                nav = null;
            }
            catch
            {
            }
        }
    }
}

Build project then click F5, result as below


Deploy an InfoPath 2010 Form with Managed Code to a Browser Enabled Sharepoint Document Library

0 comments:

Post a Comment