So I'm trying to get the label out of the terms tagged in a sharepoint list but as we all know there is a bug that you can't get the label using rest API if the field isn't set to multivalue...So I'm getting it to multivalue but I'm still unable to get the value as it results undefined...Any idea?
2 Answers
You dont need to make it multi-valued anymore to get the label values.
You can use the new first-class RenderListDataAsStream
API to get the taxonomy label values. Instead of a standard GET
call to the SharePoint list, here we will be making a POST
call.
Modify the code from the below snippet. Use the internal name of columns in the ViewXml
.
protected getListData(): void {
const restAPI = `${this.context.pageContext.web.absoluteUrl}/_api/web/Lists/GetByTitle('Documents')/RenderListDataAsStream`;
this.context.spHttpClient.post(restAPI, SPHttpClient.configurations.v1, {
body: JSON.stringify({
parameters: {
RenderOptions: 2,
ViewXml: `<View>
<ViewFields>
<FieldRef Name="Title"/>
<FieldRef Name="YourManagedMetadataField"/>
</ViewFields>
</View>`
}
})
})
.then((response: SPHttpClientResponse) => response.json())
.then((response: any) => {
if (response && response.Row && response.Row.length > 0) {
var rows = [];
response.Row.forEach(element => {
rows.push({
Title: element.Title,
Test: element.YourManagedMetadataField[0].Label //do null handling
})
});
}
});
}
Referenced and modified from - Using the SharePoint RenderListDataAsStream API to fetch lookup and single managed metadata field values
You need to include the TaxCatchAll field which contains the label, then lookup that field for your returned term id:
http://sympmarc.com/2017/06/19/retrieving-multiple-sharepoint-managed-metadata-columns-via-rest/