Choice Fields |
Choice fields restrict a field's values to a predefined list. ReadySuite supports two types: Choice (single value) and MultiChoice (multiple values). This topic covers creating choice fields, managing their value lists, and reading or writing choice data on documents.
Use CreateField() or GetOrCreateField() with a ChoiceFieldItemType:
script.CreateField("Status", ChoiceFieldItemType.Choice); script.CreateField("Tags", ChoiceFieldItemType.MultiChoice);
GetOrCreateField() returns the existing field if it already exists:
var field = script.GetOrCreateField("Status", ChoiceFieldItemType.Choice);
To work with a choice field's value list, convert it to IChoiceFieldItem using AsChoiceFieldItem():
var field = script.GetField("Status"); var choiceField = field.AsChoiceFieldItem();
AsChoiceFieldItem() returns null if the field is not a choice field.
Add allowed values individually or in bulk:
choiceField.AddValue("Open", "Closed", "Pending");
You can also add values from a delimited string:
choiceField.AddValue("Open;Closed;Pending", ';');
if (choiceField.HasValues()) { var values = choiceField.GetValues(); // Returns the list of allowed values }
Remove a single value or clear all values:
choiceField.DeleteValue("Pending");
choiceField.ClearValues();For single-choice fields, set a value with SetFieldData():
document.SetFieldData(options.StatusField, "Open");
script.UpdateDocument(document);For multi-choice fields, pass a collection of values:
var tags = new List<string> { "Responsive", "Privileged" }; document.SetFieldData(choiceField, tags, addIfMissingValue: true); script.UpdateDocument(document);
You can also set multi-choice values from a delimited string:
document.SetFieldData(choiceField, "Responsive;Privileged", ';', addIfMissingValue: true); script.UpdateDocument(document);
When addIfMissingValue is true, any value not already in the choice list is added automatically. When false, values not in the list are ignored.
Reading choice data works the same as any other field:
var value = document.GetFieldData(options.StatusField);
For multi-choice fields, the returned string contains the selected values in their stored format.
This example creates a single-choice "ReviewStatus" field, populates its value list, and assigns a value to each document:
public override void Run(IScriptContext script) { base.Run(script); var field = script.GetOrCreateField("ReviewStatus", ChoiceFieldItemType.Choice); var choiceField = field.AsChoiceFieldItem(); choiceField.AddValue("Not Started", "In Progress", "Complete"); while (script.ReadDocument()) { var document = script.Document; var current = document.GetFieldData(field); if (string.IsNullOrEmpty(current)) { document.SetFieldData(field, "Not Started"); script.UpdateDocument(document); } } }