0

I've a list in SPFx with a column type of Managed metadata. Now I'll add an item in that list with a web part. But how can I do this?

import { TaxonomyPicker, IPickerTerm } from "@pnp/spfx-controls-react/lib/TaxonomyPicker";

public async addNew(product: Product): Promise<void> {
    const resp = await this
        .web
        .lists
        .getById('xxx')
        .items
        .add({
            Title: product.title,
            Category: product.category ? {
                TermGuid: product.category.key,
                Label: product.category.name
            } : null
        });

    console.log(resp);
}

product.category is type of IPickerTerm comming from TaxonomyPicker of my React component.

Code above doesn't throw an error but when I selected the added item again, the category is empty.

<d:Category m:type="SP.Taxonomy.TaxonomyFieldValue">
    <d:Label>0</d:Label>
    <d:TermGuid></d:TermGuid>
    <d:WssId m:type="Edm.Int32">0</d:WssId>
</d:Category>

Do I miss anything here?

1

I must send this with __metadata inside Category and with a WssId:

let toSend: any = {
    Title: product.title,
    Category: product.category ? {
        __metadata: {
            type: 'SP.Taxonomy.TaxonomyFieldValue'
        },
        TermGuid: product.category.key,
        Label: product.category.name,
        WssId: -1
    } : null
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.