In below code we will see, how to add multichoice chekcbox field in list, add item with checkbox field and get value from chekcbox field.
using System;
using System.Collections.Specialized;
using Microsoft.SharePoint;
namespace ConsoleApp
{
class Program
{
static string listTitle = "My Custom List";
static string fieldTitle = "Gift Options";
static string fieldInternalName = null;
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList(listTitle);
if (list != null)
{
// Add a multichoice field to the list.
StringCollection choices = new StringCollection();
choices.Add("Gift wrap");
choices.Add("Gift card");
choices.Add("Include gift receipt");
fieldInternalName = list.Fields.Add(fieldTitle, SPFieldType.MultiChoice, false, false, choices);
list.Update();
// Get a reference to the field.
SPFieldMultiChoice choiceField = (SPFieldMultiChoice)list.Fields.GetField(fieldInternalName);
// Create a field value with all choices selected.
// (A CheckBoxChoiceField control would have all boxes checked.)
SPFieldMultiChoiceValue values = new SPFieldMultiChoiceValue();
foreach (string choice in choices)
{
values.Add(choice);
}
// Add an item to the list.
SPListItem item = list.Items.Add();
item[SPBuiltInFieldId.Title] = "Test item";
item[choiceField.Id] = values;
item.Update();
// Get the value of the field in the item.
string rawValue = item[choiceField.Id].ToString();
SPFieldMultiChoiceValue typedValue = new SPFieldMultiChoiceValue(rawValue);
// Print the value.
Console.WriteLine("The raw value is {0}", rawValue);
Console.WriteLine("The value delimiter is {0}", SPFieldMultiChoiceValue.Delimiter);
for (int i = 0; i < typedValue.Count; i++)
{
Console.WriteLine("The value at index {0} is {1}", i, typedValue[i]);
}
}
}
}
Console.WriteLine("\nPress ENTER to continue....");
Console.Read();
}
}
}
Hope this helps!!!
using System;
using System.Collections.Specialized;
using Microsoft.SharePoint;
namespace ConsoleApp
{
class Program
{
static string listTitle = "My Custom List";
static string fieldTitle = "Gift Options";
static string fieldInternalName = null;
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList(listTitle);
if (list != null)
{
// Add a multichoice field to the list.
StringCollection choices = new StringCollection();
choices.Add("Gift wrap");
choices.Add("Gift card");
choices.Add("Include gift receipt");
fieldInternalName = list.Fields.Add(fieldTitle, SPFieldType.MultiChoice, false, false, choices);
list.Update();
// Get a reference to the field.
SPFieldMultiChoice choiceField = (SPFieldMultiChoice)list.Fields.GetField(fieldInternalName);
// Create a field value with all choices selected.
// (A CheckBoxChoiceField control would have all boxes checked.)
SPFieldMultiChoiceValue values = new SPFieldMultiChoiceValue();
foreach (string choice in choices)
{
values.Add(choice);
}
// Add an item to the list.
SPListItem item = list.Items.Add();
item[SPBuiltInFieldId.Title] = "Test item";
item[choiceField.Id] = values;
item.Update();
// Get the value of the field in the item.
string rawValue = item[choiceField.Id].ToString();
SPFieldMultiChoiceValue typedValue = new SPFieldMultiChoiceValue(rawValue);
// Print the value.
Console.WriteLine("The raw value is {0}", rawValue);
Console.WriteLine("The value delimiter is {0}", SPFieldMultiChoiceValue.Delimiter);
for (int i = 0; i < typedValue.Count; i++)
{
Console.WriteLine("The value at index {0} is {1}", i, typedValue[i]);
}
}
}
}
Console.WriteLine("\nPress ENTER to continue....");
Console.Read();
}
}
}
Hope this helps!!!
No comments:
Post a Comment