Skip to main content
Box Employee
May 22, 2025 11:56 AM UTC
Question

Unable to select the items in multiselect through code

Hi,

We have created a template with multiselect control as below: 

var templateParams = new BoxMetadataTemplate()
{
TemplateKey = "multiselecttest",
DisplayName = "Multiselect Test",
Scope = "enterprise",
Fields = new List<BoxMetadataTemplateField>()
{
new BoxMetadataTemplateField()
{
Type = "multiSelect",
Key = "author",
DisplayName = "Author",
Options = new List<BoxMetadataTemplateFieldOption>()
{
new BoxMetadataTemplateFieldOption() { Key = "Author1" },
new BoxMetadataTemplateFieldOption() { Key = "Author2" },
new BoxMetadataTemplateFieldOption() { Key = "Author3" },
new BoxMetadataTemplateFieldOption() { Key = "Author4" },
new BoxMetadataTemplateFieldOption() { Key = "Author5" },
new BoxMetadataTemplateFieldOption() { Key = "janani" },
}
}
}
};

BoxMetadataTemplate template1 = await _boxClient.MetadataManager.CreateMetadataTemplate(templateParams);

We applied the above template to a specific file and tried to select few items in the control:

var templateParams = new List<BoxMetadataUpdate>()
{
new BoxMetadataUpdate()
{
Op = MetadataUpdateOp.add,
Path = "/author",
Value = "["Author2","Author3"]"
}
};

Dictionary<string, object> updatedMetadata = await _boxClient.MetadataManager.UpdateFileMetadataAsync(123456, templateParams, "enterprise", "multiselecttest");

We are getting the below error:

The API returned an error [BadRequest | f24yzjgucw9wt1ud.07d752f31b93145c314a9080315bc15f2] schema_validation_failed - instance type (string) does not match any allowed primitive type (allowed: ["array"]) (pointer: /author)

 

    2 replies

    Box Employee
    May 22, 2025 11:56 AM UTC

    I tried with the below also and getting the same error.

    List<string> lstSelectedAuthors = new List<string>() { "Author2", "Author3" };
    var metadataValues = new Dictionary<string, object>();

    metadataValues.Add("author", JsonConvert.SerializeObject(lstSelectedAuthors));

    var metadata = await _boxClient.MetadataManager.SetFileMetadataAsync(123456, metadataValues, "enterprise", "multiselecttest");

    Translate
    Box Employee
    May 22, 2025 11:56 AM UTC

    Hi Vijay, 

    Thank you for bringing this up! I have an answer for you. You will want to create a string array with the options you want. 



    Hope this helps! 

    Thanks, 

    Alex, Box Developer Advocate

    private static async Task CreateMetadataTemplateInstance(BoxClient adminClient)
    {
    string[] selectedOptions = { "Author1", "Author2"};
    var metadataValues = new Dictionary<string, object>()
    {
    { "author", selectedOptions }
    };

    var metadata = await adminClient.MetadataManager.SetFileMetadataAsync(fileId: "123456", metadataValues, "enterprise", "multiselecttest");

    Console.WriteLine("Instance Added To File: ", metadata.ToString());

    }
    Translate