Read sharepoint list data using javascript managed metadata column
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript">
var siteUrl = '/';
function createListItem() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Mylist');
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('Mylist');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', 'Test');
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onCreateSucceeded), Function.createDelegate(this, this.onCreateFailed));
}
}
function onCreateSucceeded() {
alert('Item created: ' + oListItem.get_id());
}
}
function onCreateFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
function retrieveListItems() {
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyList');
var clientContext = new SP.ClientContext(siteUrl);
var oList = clientContext.get_web().get_lists().getByTitle('MyList');
var camlQuery = new SP.CamlQuery();
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onRetrieveySucceeded), Function.createDelegate(this, this.onRetrieveFailed));
}
function onRetrieveySucceeded(sender, args) {
var listItemInfo = '';
var listItemEnumerator = collListItem.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
//listItemInfo += '\nTitle: ' + oListItem.get_item('Title') ;
listItemInfo += '\nTitle: ' + oListItem.get_item('ManagedMetaData')["Label"];
}
var oListItem = listItemEnumerator.get_current();
//listItemInfo += '\nTitle: ' + oListItem.get_item('Title') ;
listItemInfo += '\nTitle: ' + oListItem.get_item('ManagedMetaData')["Label"];
}
alert(listItemInfo.toString());
}
}
function onRetrieveFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}
</script>
<input type="button" id="btnGet" onclick="retrieveListItems(); return false;" value = "Get Item" />
<input type="button" id="btnAdd" onclick="createListItem(); return false;" value = "Add Item" />