Collecting formfields and sdt dropdown values

Hi, I am creating a Word document that has several textboxes and comboboxes that our users download as Word file and then upload back into our web app and text and values are inserted into our database.

I have it working really well except, the last dropdown on my form needs to have more than 25 entries. I can’t use builder.InsertComboBox for this, so I figured out how to insert a combobox with StructuredDocumentTag. However, I can’t retrieve the value selected once the document is saved. Here is code I am using:

Document doc = new Document(path + filename);
foreach (FormField field in doc.Range.FormFields)
{
    if (field.Type == FieldType.FieldFormTextInput)
        Console.WriteLine(field.Name + " : " + field.Result);
    if (field.Type == FieldType.FieldFormDropDown)
        Console.WriteLine(field.Name + " : " + field.Result);
}

foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    if (sdt.SdtType == SdtType.ComboBox)
    {
        SdtListItem selectedItem = sdt.ListItems.SelectedValue;
        Console.WriteLine("Selected from SDT Combobox: " + selectedItem);
    }
}

I get a list of all my formfields without a problem but for sdt combobox value, I get this:
Selected from SDT Combobox: Aspose.Words.Markup.SdtListItem

What am I doing wrong?
I am so close to having it all work, can anyone help?

@protstein You should use ListItems.SelectedValue.Value to get value of the selected item in SDT combo box. Please see the following code:

Document doc = new Document(@"C:\Temp\in.docx");

// Get combobox.
StructuredDocumentTag combo = doc.GetChildNodes(NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>()
    .Where(sdt => sdt.SdtType == SdtType.ComboBox).First();

// Print the value of the selected item
Console.WriteLine("Selected from SDT Combobox: " + combo.ListItems.SelectedValue.Value);

Thank you @alexey.noskov! That worked perfectly!

1 Like