Adding Data to New Managed MetaData Field
var Tower = [];
var Category = [];
var info = [];
var LoadDocData = [];
var exect=false;
$(document).ready(function () {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", function () {
$.getScript(scriptbase + "SP.Taxonomy.js", function () {
context = SP.ClientContext.get_current();
//Call your code here.
execOperation();
});
});
});
});
function execOperation() {
//Current Context
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Term Stores
var termStoreColl = taxSession.get_termStores();
//Name of the Term Store from which to get the Terms.
var termStoreT = termStoreColl.getByName("Managed Metadata Service");
//GUID of Term Set from which to get the Terms.
var termSetT = termStoreT.getTermSet("257bfe19-ff48-482f-bc22-72cd5f471320");
var termsT = termSetT.getAllTerms();
context.load(termsT);
context.executeQueryAsync(function () {
var termEnumerator = termsT.getEnumerator();
var termList = "Terms: \n";
while (termEnumerator.moveNext()) {
var currentTerm = termEnumerator.get_current();
termList += currentTerm.get_name() + "\n";
var currentTermValue = currentTerm.get_pathOfTerm().split(';');
var currentTermId = currentTerm.get_id().toString();
if (currentTermValue.length == 1) {
var temTower = {
Tower: currentTermValue[0],
TowerId: currentTermId
};
Tower.push(temTower);
}
else if (currentTermValue.length == 2) {
var tempCat = {
Tower: currentTermValue[0],
Category: currentTermValue[1],
CategoryId: currentTermId
}
Category.push(tempCat);
}
else if (currentTermValue.length == 3) {
var tempInfo = {
Tower: currentTermValue[0],
Category: currentTermValue[1],
info: currentTermValue[2],
infoId: currentTermId
}
info.push(tempInfo);
}
}
console.log(termList);
}, function (sender, args) {
console.log(args.get_message());
});
}
function CreateItem(){
CreateListItems(Tower.length,Category.length, info.length,0);
}
function CreateListItems(T,C,Inn,II) {
var itemArray = [];
var clientContextUpdate = SP.ClientContext.get_current();
var oList = clientContextUpdate.get_web().get_lists().getByTitle('Create WSSID');
var itemCreateInfo;
var it = 0;
for (var i = 0; i < T ; i++) {
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', Tower[i].Tower);
var field = oList.get_fields().getByInternalNameOrTitle("Tower");
var taxField = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();
taxonomySingle.set_label(Tower[i].Tower);
taxonomySingle.set_termGuid(Tower[i].TowerId);
taxonomySingle.set_wssId(-1);
taxField.setFieldValueByValue(oListItem, taxonomySingle);
oListItem.update();
itemArray[it] = oListItem;
clientContextUpdate.load(itemArray[it]);
it++;
}
for (var i = 0; i < C ; i++) {
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', Category[i].Category);
var field = oList.get_fields().getByInternalNameOrTitle("Category");
var taxField = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();
taxonomySingle.set_label(Category[i].Category);
taxonomySingle.set_termGuid(Category[i].CategoryId);
taxonomySingle.set_wssId(-1);
taxField.setFieldValueByValue(oListItem, taxonomySingle);
oListItem.update();
itemArray[it] = oListItem;
clientContextUpdate.load(itemArray[it]);
it++;
}
for (var i = II; i < Inn ; i++) {
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', info[i].info);
var field = oList.get_fields().getByInternalNameOrTitle("Info");
var taxField = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();
taxonomySingle.set_label(info[i].info);
taxonomySingle.set_termGuid(info[i].infoId);
taxonomySingle.set_wssId(-1);
taxField.setFieldValueByValue(oListItem, taxonomySingle);
oListItem.update();
itemArray[it] = oListItem;
clientContextUpdate.load(itemArray[it]);
it++;
}
clientContextUpdate.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
alert('Items Updated');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
var Tower = [];
var Category = [];
var info = [];
var LoadDocData = [];
var exect=false;
$(document).ready(function () {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", function () {
$.getScript(scriptbase + "SP.Taxonomy.js", function () {
context = SP.ClientContext.get_current();
//Call your code here.
execOperation();
});
});
});
});
function execOperation() {
//Current Context
var context = SP.ClientContext.get_current();
//Current Taxonomy Session
var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);
//Term Stores
var termStoreColl = taxSession.get_termStores();
//Name of the Term Store from which to get the Terms.
var termStoreT = termStoreColl.getByName("Managed Metadata Service");
//GUID of Term Set from which to get the Terms.
var termSetT = termStoreT.getTermSet("257bfe19-ff48-482f-bc22-72cd5f471320");
var termsT = termSetT.getAllTerms();
context.load(termsT);
context.executeQueryAsync(function () {
var termEnumerator = termsT.getEnumerator();
var termList = "Terms: \n";
while (termEnumerator.moveNext()) {
var currentTerm = termEnumerator.get_current();
termList += currentTerm.get_name() + "\n";
var currentTermValue = currentTerm.get_pathOfTerm().split(';');
var currentTermId = currentTerm.get_id().toString();
if (currentTermValue.length == 1) {
var temTower = {
Tower: currentTermValue[0],
TowerId: currentTermId
};
Tower.push(temTower);
}
else if (currentTermValue.length == 2) {
var tempCat = {
Tower: currentTermValue[0],
Category: currentTermValue[1],
CategoryId: currentTermId
}
Category.push(tempCat);
}
else if (currentTermValue.length == 3) {
var tempInfo = {
Tower: currentTermValue[0],
Category: currentTermValue[1],
info: currentTermValue[2],
infoId: currentTermId
}
info.push(tempInfo);
}
}
console.log(termList);
}, function (sender, args) {
console.log(args.get_message());
});
}
function CreateItem(){
CreateListItems(Tower.length,Category.length, info.length,0);
}
function CreateListItems(T,C,Inn,II) {
var itemArray = [];
var clientContextUpdate = SP.ClientContext.get_current();
var oList = clientContextUpdate.get_web().get_lists().getByTitle('Create WSSID');
var itemCreateInfo;
var it = 0;
for (var i = 0; i < T ; i++) {
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', Tower[i].Tower);
var field = oList.get_fields().getByInternalNameOrTitle("Tower");
var taxField = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();
taxonomySingle.set_label(Tower[i].Tower);
taxonomySingle.set_termGuid(Tower[i].TowerId);
taxonomySingle.set_wssId(-1);
taxField.setFieldValueByValue(oListItem, taxonomySingle);
oListItem.update();
itemArray[it] = oListItem;
clientContextUpdate.load(itemArray[it]);
it++;
}
for (var i = 0; i < C ; i++) {
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', Category[i].Category);
var field = oList.get_fields().getByInternalNameOrTitle("Category");
var taxField = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();
taxonomySingle.set_label(Category[i].Category);
taxonomySingle.set_termGuid(Category[i].CategoryId);
taxonomySingle.set_wssId(-1);
taxField.setFieldValueByValue(oListItem, taxonomySingle);
oListItem.update();
itemArray[it] = oListItem;
clientContextUpdate.load(itemArray[it]);
it++;
}
for (var i = II; i < Inn ; i++) {
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', info[i].info);
var field = oList.get_fields().getByInternalNameOrTitle("Info");
var taxField = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();
taxonomySingle.set_label(info[i].info);
taxonomySingle.set_termGuid(info[i].infoId);
taxonomySingle.set_wssId(-1);
taxField.setFieldValueByValue(oListItem, taxonomySingle);
oListItem.update();
itemArray[it] = oListItem;
clientContextUpdate.load(itemArray[it]);
it++;
}
clientContextUpdate.executeQueryAsync(onQuerySucceeded, onQueryFailed);
}
function onQuerySucceeded() {
alert('Items Updated');
}
function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
No comments:
Post a Comment