SOLVED

SharePoint Online Rest API update managed metadata columns

%3CLINGO-SUB%20id%3D%22lingo-sub-1618337%22%20slang%3D%22de-DE%22%3ESharePoint%20Online%20Rest%20API%20update%20managed%20metadata%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1618337%22%20slang%3D%22de-DE%22%3E%3CP%3EHello%20Community%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20implement%20an%20API%20based%20file%20upload%20using%20the%20following%20Microsoft%20tutorial%3A%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fde-de%2Fsharepoint%2Fdev%2Fsp-add-ins%2Fupload-a-file-by-using-the-rest-api-and-jquery%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fde-de%2Fsharepoint%2Fdev%2Fsp-add-ins%2Fupload-a-file-by-using-the-rest-api-and-jquery%3C%2FA%3E%3C%2FP%3E%3CP%3EThe%20upload%20is%20already%20working%20and%20I'm%20stuck%20on%20the%20last%20step%2C%20updating%20the%20file%20metadata.%3C%2FP%3E%3CP%3EIt%20was%20already%20possible%20for%20me%20to%20update%20simple%20text%20lines.%20%3CBR%20%2F%3E%20But%20since%20I%20unfortunately%20have%20to%20update%20managed%20meta%20data%20columns%20as%20well%2C%20I%20am%20now%20stuck%20with%20this%20challenge.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThe%20itemMetadata%20variable%20was%20set%20as%20follows%20and%20contains%20the%20following%20data%20of%20the%20file%3A%20%3CBR%20%2F%3E%20-%20%3CSPAN%3Eetag%20%3CBR%20%2F%3E%20%3C%2FSPAN%3E-%20%3CSPAN%3Eid%20%3CBR%20%2F%3E%20%3C%2FSPAN%3E-%20type%20-%20%3CSPAN%3E%20%3CBR%20%2F%3E%20uri%3C%2FSPAN%3E%3CBR%20%2F%3E_%20code%20to%20set%20var%20itemMetadata%3C%2FP%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3EitemMetadata%20%3D%20return%20jQuery.ajax(%7B%0A%20%20%20%20%20%20url%3A%20fileListItemUri%2C%0A%20%20%20%20%20%20type%3A%20%22GET%22%2C%0A%20%20%20%20%20%20headers%3A%20%7B%20%22accept%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%20%7D%0A%20%20%20%20%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSTRONG%3ENow%20to%20the%20crux%20of%20the%20matter.%3C%2FSTRONG%3E%3CBR%20%2F%3E%20I%20set%20the%20variable%20body%20as%20follows%20to%20update%20the%20metadata.%20%3CBR%20%2F%3E%20The%20variable%20%3CU%3Emd%3C%2FU%3E%20contains%20the%20metadata%20I%20want%20to%20update.%20this%20is%20from%20a%20sharepoint%20list%20consisting%20of%20the%20same%20taxonomy%20store.%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Evar%20body%20%3D%20JSON.stringify(%7B%0A%22__metadata%22%3A%7B%22type%22%3AitemMetadata.type%7D%2C%22Titke%22%3Amd.Title%2C%22MyManagedMetadataField%22%3A%7B%22__metadata%22%3A%7B%22type%22%3A%22SP.Taxonomy.TaxonomyFieldValue%22%7D%2C%0A%22Label%22%3Amd.metadata.Label%2C%22TermGuid%22%3Amd.metadata.TermGuid%2C%22WssId%22%3A-1%7D%0A%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EThen%20the%20update%20is%20triggered%20as%20follows.%3C%2FDIV%3E%3CDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Ereturn%20jQuery.ajax(%7B%0A%20%20%20%20%20%20%20%20url%3A%20itemMetadata.uri%2C%0A%20%20%20%20%20%20%20%20type%3A%20%22POST%22%2C%0A%20%20%20%20%20%20%20%20data%3A%20body%2C%0A%20%20%20%20%20%20%20%20headers%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%22X-RequestDigest%22%3A%20reqDigest%2C%0A%20%20%20%20%20%20%20%20%20%20%22content-type%22%3A%20%22application%2Fjson%3Bodata%3Dverbose%22%2C%0A%20%20%20%20%20%20%20%20%20%20%2F%2F%20%22content-length%22%3A%20body.length%2C%0A%20%20%20%20%20%20%20%20%20%20%22IF-MATCH%22%3A%20itemMetadata.etag%2C%0A%20%20%20%20%20%20%20%20%20%20%22X-HTTP-Method%22%3A%20%22MERGE%22%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20success%3A%20(xhr)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20%20%20deferred.resolve()%3B%0A%20%20%20%20%20%20%20%20%20%20console.log(%22success!%3F!%22)%3B%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20error%3A%20(xhr)%20%3D%26gt%3B%20%7B%0A%20%20%20%20%20%20%20%20%20%20console.log(xhr.responseText)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EI%20get%20no%20error%20and%20the%20title%20field%20is%20also%20updated%20as%20expected.%20Only%20the%20Managed%20Metadata%20field%20remains%20empty.%20The%20consoles%20log%20%22success!%3F!%22%20is%20displayed.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20I%20have%20already%20read%20in%20other%20forums%20that%20this%20call%20does%20not%20work%20and%20it%20was%20suggested%20to%20use%20JSOM.%20but%20in%20my%20opinion%20this%20is%20not%20a%20solution%2C%20especially%20since%20it%20is%20described%20in%20the%20original%20Microsoft%20article.%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EDid%20anyone%20succeed%3F%20%3CBR%20%2F%3E%20cheers%20Saintenr%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20%3CEM%3EThis%20code%20is%20executed%20in%20a%20Web%20Part%20in%20the%20context%20of%20the%20Sharepoint%20online%20workbench.%3C%2FEM%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1618337%22%20slang%3D%22de-DE%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDocument%20Library%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFiles%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EUsage%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1671736%22%20slang%3D%22de-DE%22%3ESubject%3A%20SharePoint%20Online%20Rest%20API%20update%20managed%20metadata%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1671736%22%20slang%3D%22de-DE%22%3EHello%2C%20an%20update%20from%20me.%20%3CBR%20%2F%3E%20The%20problem%20why%20the%20metadata%20columns%20could%20not%20be%20updated%20was%20that%20the%20Document%20Library%20had%20default%20values%20set%20for%20folders%20and%20subfolders.%20If%20this%20function%20is%20active%2C%20no%20managed%20metadata%20column%20can%20be%20updated%20via%20the%20api...%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Possible%20solution%3A%20%3CBR%20%2F%3E%20Delete%20this%20default%20value%20rules%20for%20every%20folder%20on%20the%20library.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Hope%20this%20will%20help%20other%20users%20too.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%20Cheers%20Saintenr%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1720612%22%20slang%3D%22de-DE%22%3ESubject%3A%20SharePoint%20Online%20Rest%20API%20update%20managed%20metadata%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1720612%22%20slang%3D%22de-DE%22%3E%3CP%3EAfter%20contact%20with%20Microsoft%20support%20we%20have%20come%20to%20the%20conclusion%20that%20it%20is%20not%20necessary%20to%20delete%20the%20default%20values%20for%20the%20columns.%20The%20following%20format%20is%20possible%20and%20works%20fine.%20%3CBR%20%2F%3E%20The%20URI%20only%20needs%20to%20be%20extended%20by%20one%20function.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%3E%3CDIV%3E%3CSPAN%3EitemMetadata%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%20%3CSPAN%3Euri%3C%2FSPAN%3E%3CSPAN%3E%2B%3C%2FSPAN%3E%3CSPAN%3E'%2FValidateUpdateListItem()%3C%2FSPAN%3E%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%3CSPAN%3EThe%20ValidateUpdateListItem%20function%20needs%20this%20json%20format%20as%20body%3A%3C%2FSPAN%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-json%22%3E%3CCODE%3E%7B%0A%22formValues%22%3A%0A%7B%0A%22FieldName%22%3A%26nbsp%3B%22Metadatacolumn%22%2C%26nbsp%3B%0A%22FieldValue%22%3A%26nbsp%3BtaxItemLabel%26nbsp%3B%2B%26nbsp%3B'%7C'%26nbsp%3B%2B%26nbsp%3BtaxItemTermGuid%0A%7D%2C%0A%22bNewDocumentUpdate%22%3A%26nbsp%3Bfalse%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%3C%2FP%3E%3CP%3ESaintenr%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1991513%22%20slang%3D%22en-US%22%3EBetreff%3A%20SharePoint%20Online%20Rest%20API%20update%20managed%20metadata%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1991513%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F774460%22%20target%3D%22_blank%22%3E%40Saintenr%3C%2FA%3E%26nbsp%3BHello%2C%20I%20am%20in%20the%20process%20of%20migrating%20data%20to%20sharepoint%20and%20have%20a%20requirement%20to%20update%20the%20metadata%20for%20managed%20MetaData%20fields%20for%20documents%20uploaded.%20I%20need%20to%20download%20the%20TermGuid%20for%20all%20the%20items%20in%20the%20TermSet.%20I%20will%20have%20to%20then%20pass%20the%20TermGuid%20and%20Label%20to%20the%20update%20document%20API%20call.%20I%20do%20not%20see%20any%20REST%20API%20avaiable%20to%20download%20the%26nbsp%3B%26nbsp%3BTermSet%20information%20for%20a%20specific%20managed%20meta%20data%20field.%20In%20your%20code%20example%20I%20see%20that%20you%20already%20have%20this%20information.%20Where%20and%20how%20are%20you%20getting%20this%20information.%20It%20would%20be%20really%20helpful%20if%20you%20can%20tell%20me%20how%20you%20are%20getting%20this%20information.%20Thanks%20so%20much%20for%20your%20help!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2201860%22%20slang%3D%22de-DE%22%3ESubject%3A%20SharePoint%20Online%20Rest%20API%20update%20managed%20metadata%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2201860%22%20slang%3D%22de-DE%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F903171%22%20target%3D%22_blank%22%3E%40shamala2020%3C%2FA%3E%20sorry%20for%20the%20late%20response.%20You%20have%20to%20select%20these%20columns%20in%20your%20request%3A%20%3CBR%20%2F%3E%20TaxCatchAll%2FID%20%3CBR%20%2F%3E%20TaxCatchAll%2FTerm%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello Community,

 

I am trying to implement an API based file upload using the following Microsoft tutorial: https://docs.microsoft.com/de-de/sharepoint/dev/sp-add-ins/upload-a-file-by-using-the-rest-api-and-j...

The upload is already working and I'm stuck on the last step, updating the file metadata.

It was already possible for me to update simple text lines.
But since I unfortunately have to update managed meta data columns as well, I am now stuck with this challenge.


The itemMetadata variable was set as follows and contains the following data of the file:
etag
id
type
- uri

// code to set var itemMetadata

 

 

itemMetadata = return jQuery.ajax({
      url: fileListItemUri,
      type: "GET",
      headers: { "accept": "application/json;odata=verbose" }
    });

 



Now to the crux of the matter.
I set the variable body as follows to update the metadata.
The variable md contains the metadata I want to update. this is from a sharepoint list consisting of the same taxonomy store.
 
 

 

var body = JSON.stringify({
"__metadata":{"type":itemMetadata.type},"Titke":md.Title,"MyManagedMetadataField":{"__metadata":{"type":"SP.Taxonomy.TaxonomyFieldValue"},
"Label":md.metadata.Label,"TermGuid":md.metadata.TermGuid,"WssId":-1}
});

 

 
Then the update is triggered as follows.
 
 

 

return jQuery.ajax({
        url: itemMetadata.uri,
        type: "POST",
        data: body,
        headers: {
          "X-RequestDigest": reqDigest,
          "content-type": "application/json;odata=verbose",
          // "content-length": body.length,
          "IF-MATCH": itemMetadata.etag,
          "X-HTTP-Method": "MERGE"
        },
        success: (xhr) => {
          deferred.resolve();
          console.log("success!?!");
        },
        error: (xhr) => {
          console.log(xhr.responseText);
        }
    });

 

 
I get no error and the Title field is also updated as expected. Only the Managed Metadata field remains empty. The consoles log "success!?!" is displayed.

I have already read in other forums that this call does not work and it was suggested to use JSOM. but in my opinion this is not a solution, especially since it is described in the original Microsoft article.
 
 
Did anyone succeed?
cheers Saintenr

This code is executed in a Web Part in the context of the Sharepoint online workbench.

4 Replies
Hello, an update from me.
The problem why the metadata columns could not be updated was that the Document Library had default values set for folders and subfolders. If this function is active, no managed metadata column can be updated via the api...

Possible solution:
Delete this default value rules for every folder on the library.

Hope this will helps other users too.

Cheers Saintenr
best response confirmed by Saintenr (Occasional Contributor)
Solution

After contact with Microsoft support we have come to the conclusion that it is not necessary to delete the default values for the columns. The following format is possible and works fine.
The URI only needs to be extended by one function. 

 

itemMetadata.uri+'/ValidateUpdateListItem()
 
The ValidateUpdateListItem function needs this json format as body:
{
"formValues":
{
"FieldName": "Metadatacolumn", 
"FieldValue": taxItemLabel + '|' + taxItemTermGuid
},
"bNewDocumentUpdate": false
}

 

Cheers,

Saintenr

@Saintenr Hello, I am in the process of migrating data to sharepoint and have a requirement to update the metadata for managed MetaData fields for documents uploaded. I need to download the TermGuid for all the items in the TermSet. I will have to then pass the TermGuid and Label to the update document API call. I do not see any REST API avaiable to download the  TermSet information for a specific managed meta data field. In your code example I see that you already have this information. Where and how are you getting this information. It would be really helpful if you can tell me how you are getting this information. Thanks so much for your help!

@shamala2020 sorry for the late response. You have to select these columns in your request:
TaxCatchAll/ID
TaxCatchAll/Term