5

I'm not able to modify taxonomy field values with REST API or JSOM for a specific library. My code works on other lists, but not on this particular library, what can the problem be?

  • The library has > 380 000 items.
  • Field can be changed via edit item dialog.
  • No error messages (REST API or JSOM).
  • Field is single value only, so no "_0"-problem.
    • Have verified there is no Single_0 (by title) field.
  • I want to use odata=verbose, but I have tried without it (and removed all the __metadata properties), with same result.
  • Updating other fields, such as Title, works (in same request, both JSOM and REST API).
  • I have used edit item dialog to change the value back and forth to verify that the term ID I'm trying to use is without doubt the correct one.
  • "Nulling" out the field does not work either (e.g., { Single: null }).
  • Have tried hard coding correct WssId and Label, but no luck.

My REST API-code looks like this:

fetch(`${_spPageContextInfo.webAbsoluteUrl}/_api/Web/Lists/GetByTitle('TaxonomyTest')/Items(1)`, {
  credentials: 'include',
  method: 'MERGE',
  headers: {
    'content-type': 'application/json;odata=verbose',
    'x-requestdigest': _spPageContextInfo.formDigestValue,
    'if-match': '*'
  },
  body: JSON.stringify({
    __metadata: {
      type: 'SP.Data.TaxonomyTestListItem'
    },
    Single: {
      __metadata: {
        type: 'SP.Taxonomy.TaxonomyFieldValue',
      },
      WssId: -1,
      TermGuid: '10114d7f-b240-4b93-a23b-15ce3e5b9bbe'
    }
  })
})

And my JSOM-code looks like this:

const ctx = SP.ClientContext.get_current()
const item = ctx.get_web().get_lists().getByTitle('TaxonomyTest').getItemById(1)
const value = new SP.Taxonomy.TaxonomyFieldValue()
value.set_termGuid('10114d7f-b240-4b93-a23b-15ce3e5b9bbe')
value.set_wssId(-1)
item.set_item('Single', value)
item.update()
ctx.executeQueryAsync()

I have not tried with CSOM.

Update

Was just able to update item using JSOM, but with a more correct approach:

const ctx = SP.ClientContext.get_current()
const list = ctx.get_web().get_lists().getByTitle('TaxonomyTest')
const item = list.getItemById(1)
const field = list.get_fields().getByTitle('Single');
const taxField = clientContext.castTo(field, SP.Taxonomy.TaxonomyField);
const value = new SP.Taxonomy.TaxonomyFieldValue()
value.set_termGuid('10114d7f-b240-4b93-a23b-15ce3e5b9bbe')
value.set_wssId(-1)
taxField.setFieldValueByValue(item, value);
item.update()
ctx.executeQueryAsync()

But still not with REST, but that might not be supported.
Note: My previous JSOM code does work on other lists, as well as the REST code. I can't mimic SetValueByValue in REST, since it does two method calls (when inspecting ProcessQuery, as well as the method is not exposed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.