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 resultColorString = "";
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