On my previous post, I went through how to create term group, term set and terms in SharePoint 2013 using javascript object model.
In this post, I will utilize that term set and then will check how to create item which contain Managed metadata Column.
Create a list will Managed Metadata Column
To update managed metadata column, you need to get the associate termSet for the column and also get the term Id from that termset.
Below is the code to add the item with managed metadata column. Read Comments in code for more description.
In this post, I will utilize that term set and then will check how to create item which contain Managed metadata Column.
Create a list will Managed Metadata Column
To update managed metadata column, you need to get the associate termSet for the column and also get the term Id from that termset.
Below is the code to add the item with managed metadata column. Read Comments in code for more description.
jQuery(function($) {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", function(){
$.getScript(scriptbase + "SP.Taxonomy.js", ExecuteTaxonomyFunctions);
});
}
);
});
function ExecuteTaxonomyFunctions()
{
var context = SP.ClientContext.get_current();
var list = context.get_web().get_lists().getByTitle("Resources");
var field = list.get_fields().getByInternalNameOrTitle("ResourceType");
var txField = context.castTo(field, SP.Taxonomy.TaxonomyField);
context.load(field);
context.load(txField);
//Get the response from server to get the termsetId
context.executeQueryAsync(function(){
//Get the term set ID
var termSetId = txField.get_termSetId().toString();
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
var termStore = taxSession.getDefaultSiteCollectionTermStore();
var termSet = termStore.getTermSet(termSetId);
// Get the matching terms based on names
var lmi = new SP.Taxonomy.LabelMatchInformation(context);
lmi.set_lcid(1033);
lmi.set_trimUnavailable(true);
lmi.set_termLabel("UI Developer");
var termMatches = termSet.getTerms(lmi);
context.load(termMatches);
//Get the matching term and then add to list item
context.executeQueryAsync(function () {
if (termMatches && termMatches.get_count() > 0)
{
// Get the first matching term. As I know it has only one
// You need to iterate and get the correct term if you have same name terms
var term = termMatches.get_item(0);
// Add Item to list
var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = list.addItem(itemCreateInfo);
oListItem.set_item('Title', 'Akash Karda');
var termFieldValue = new SP.Taxonomy.TaxonomyFieldValue();
termFieldValue.set_label(term.get_name());
termFieldValue.set_termGuid(term.get_id().toString());
termFieldValue.set_wssId(-1);
txField.setFieldValueByValue(oListItem, termFieldValue);
oListItem.update();
context.executeQueryAsync(function () {
alert('Item Added');
}, function (sender, args) {
alert(args.get_message() + '\n' + args.get_stackTrace());
});
}
}, function (sender, args) {
alert(args.get_message() + '\n' + args.get_stackTrace());
});
},function(sender,args){
alert(args.get_message());
});
}
Item Added to List
Thanks a lot you helped me !
ReplyDelete