Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

A field of type Choice populated dynamically from a SharePoint list

$
0
0

Hi,

I need to create a Choice field that would be populated from a SharePoint list. I know how to create the field and add the choice items. My question is how to add the choice items dynamically, every time the user opens the choice field list?

This is the code I'm using right now. It creates and populates a choice field. This is not what I need. I need it to be populated dynamically:

// web is SPWeb
// lib is SPDocumentLibrary

// Get choices from a list.
SPList docTypes = web.Lists.TryGetList("Category List");
var choices = new StringCollection();
foreach (SPListItem item in docTypes.Items)
    choices.Add((string)item["Title"]);

// Add the Choice field to the lib.
lib.Fields.Add("Category", SPFieldType.Choice, true, false, choices);
SPField field = lib.Fields["Category"];
field.ShowInEditForm = true;
field.ShowInNewForm = true;
field.ShowInDisplayForm = true;
field.ShowInViewForms = true;
field.Title = "Category";
field.Required = true;
field.Update();

Thanks,

Leszek


Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.com


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>