Hello,
In this post I am explaining a small topic,
how to get data from managed metadata filed using JavaScript client object
model.
The reason to write this post is, generally
if it is multi data column we check the count of the items and loop through it
to get the values of it. But here with meta data columns the syntax is little
different, we need to use child item count and loop through it and get the label property of it.
Generally we contain two types of settings
for Manged Metadata column
Single valued Metadata Column (setting: Allow
Multiple Values – False) : This contains single value
Multivalued Metadata Column (setting: Allow Multiple Values – True) : This contains single/multiple values.
Code to get the data from Singlevalued Metadata Column
Here the column name is 'ProductName'
//Single Valued Metadata column
var resultProductName = "";
var
_ProductName = listItem.get_item('ProductName')
if (typeof
_ProductName !== 'undefined' && _ProductName !== null)
{
resultProductName = listItem.get_item('ProductName').Label;
}
alert("Product Name " + resultProductName);
Code to get the data from Multivalued Metadata Column
//Multi Valued Metadata column with single/multiple values
Here the column name is 'ProductColors'
var
_ProdColors = listItem.get_item('ProductColors');
if (typeof
_ProdColors !== "undefined" && _ProdColors !== null) {
if
(_ProdColors._Child_Items_.length > 0)
{
if
(_ProdColors._Child_Items_.length == 1)// If single value
{
var
color = _ProdColors._Child_Items_[0].Label;
resultColorString = color;
}
else//
If multi value
{
for
(var i = 0; i < _ProdColors._Child_Items_.length ; i++)
{
var multicolor = _ProdColors._Child_Items_[i].Label;
if (multicolor != null) {
resultColorString = resultColorString + "," + multicolor;
}
}
if
(resultColorString.length > 0) {
resultColorString = resultColorString.substring(1,
resultColorString.length);
}
}
}
}
alert('color(s) ' + resultColorString)
Hope it saves some one's time :)
Thanks,
Purna
Hi!
ReplyDeleteIs there a possibility to use this code to color a row where a specific value is mentioned? We are using Managed Metadata and I'd like to color a row of data when a specific value is in the field of the managed metadata.
Thanks, BR Michael
Hi Michael, The ProdColors._Child_Items_[i].Label gives the metadata column value, there you can write a condition to check it is equal to your specific value. And there you can apply the style to that row if you are binding items in a table.
ReplyDeleteThanks for your answer!
DeleteBR Michael
useful post Purna
ReplyDeleteThanks,
Asif
Thanks man you saved the day.
ReplyDeleteYou welcome ..!!!
DeleteThis no longer works. _Child_Items is not exposed via JSOM any more. Use listItem.get_item('MMMultifieldname').get_item(0).get_label(); You can see in the JS om for the listItem it is not there (undefined).
ReplyDelete