Saturday, May 19, 2012

Populating InfoPath Drop Down List Box with SharePoint Fields Choice

Populate InfoPath drop-down programmatically from Sharepoint Field Choice

  • 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 Form 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: GroupStatus
Type: Group
Check to Repeating
Right click to GroupStatus | Add
Name: Displayname
Continue Right click to GroupStatus | 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, select value follows as
Add reference to Microsoft.sharepoint.dll
Using  using Microsoft.SharePoint;
Add method AddStatus (); to FormEvents_Loading
Paste this method  to below FormEvents_Loading method public void AddStatus ()
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SharePoint;

namespace SetValueToDropdownListInfoPathFromChoiceField
{
    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)
        {
            AddStatus();
        }

        public void AddStatus()
        {
            try
            {
                SPSite site = new SPSite("http://win-67038mbkel7");
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["Tasks"];
                SPFieldMultiChoice spFieldMultiChoice = (SPFieldMultiChoice)list.Fields["Status"];
                XPathNavigator nav = this.CreateNavigator().SelectSingleNode("/my:myFields/my:GroupStatus",
                    this.NamespaceManager);
                for (int item = 0; item < spFieldMultiChoice.Choices.Count; item++)
                {
                    XPathNavigator newNode = null;
                    newNode = nav.Clone();
                    newNode.SelectSingleNode("/my:myFields/my:GroupStatus/my:Displayname",
                        this.NamespaceManager).
                        SetValue(spFieldMultiChoice.Choices[item].ToString());
                    newNode.SelectSingleNode("/my:myFields/my:GroupStatus/my:Value",
                        this.NamespaceManager).
                        SetValue(spFieldMultiChoice.Choices[item].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

2 comments:

  1. Hello there, I am a fresh web page designer only starting and
    I'm in need of a portfolio. How would you like an internet
    site design with no charge?

    Feel free to visit my blog :: Is Respark The Romance a Scam

    ReplyDelete
  2. Hi I have myFields as the group and field2 as the ListBox. I am trying to populate the ListBox from a SQL query. Please help. Here is a question I asked in SO: http://stackoverflow.com/questions/31072938/how-to-clear-and-re-populate-listbox-in-a-form-from-sql-query

    ReplyDelete