Thursday, July 2, 2015

Batch Update of Managed MetaData

var Tower = [];
var Category = [];
var info = [];
var LoadDocData = [];
var TotalItration;
var initialIter = 0;
var CreateStatusOfData = [];
var SuceesData = [];
var SeleItem = 3;
_spBodyOnLoadFunctionNames.push("readyFunction");

function readyFunction() {
var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js",
function () {
$.getScript(scriptbase + "SP.js", function () {
$.getScript(scriptbase + "SP.Taxonomy.js", function () {
LoadTaxonomyHiddenListData();
});

});
});

  //  SP.SOD.executeFunc('sp.js', 'SP.ClientContext', LoadTaxonomyHiddenListData);
}

function LoadTaxonomyHiddenListData() {

    var contextList = new SP.ClientContext.get_current();
    // Load the web object

    this.web = contextList.get_web();
    //Get the list

    var list = this.web.get_lists().getByTitle('TaxonomyHiddenList');
    // Get all the items in the list

    //Load the web in the context and retrieve only selected columns to improve performance Applications                        
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query></Query></View>");
    DocData = list.getItems(camlQuery);
    contextList.load(this.DocData);
    //Make a query call to execute the above statements

    contextList.executeQueryAsync(Function.createDelegate(this, this.onSuccessList), Function.createDelegate(this, this.get_timelines_onFailure));

}
function onSuccessList() {
    // Get the collection

    var termEnumerator = this.DocData.getEnumerator();

    while (termEnumerator.moveNext()) {

        var currentTerm = termEnumerator.get_current().get_fieldValues();


        var currentTermValue = currentTerm.Path.split(':');

        var currentTermId = currentTerm.IdForTerm;
        var WSSID = currentTerm.ID;

        if (currentTermValue.length == 1) {
            var temTower = {
                Tower: currentTermValue[0],
                TowerId: currentTermId,
                WSSID: WSSID
            };
            Tower.push(temTower);
        }
        else if (currentTermValue.length == 2) {

            var tempCat = {
                Tower: currentTermValue[0],
                Category: currentTermValue[1],
                CategoryId: currentTermId,
                WSSID: WSSID
            }
            Category.push(tempCat);
        }
        else if (currentTermValue.length == 3) {
            var tempInfo = {
                Tower: currentTermValue[0],
                Category: currentTermValue[1],
                info: currentTermValue[2],
                infoId: currentTermId,
                WSSID: WSSID
            }
            info.push(tempInfo);

        }
    }
    LoadDocumentData();
}


function LoadDocumentData() {

    var contextDocList = new SP.ClientContext.get_current();
    // Load the web object

    this.webDoc = contextDocList.get_web();
    //Get the list

    var list = this.webDoc.get_lists().getByTitle('Documents');
    // Get all the items in the list

    //Load the web in the context and retrieve only selected columns to improve performance Applications                        
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml("<View><Query></Query></View>");
    timelines = list.getItems(camlQuery);
    contextDocList.load(this.timelines);


    //Make a query call to execute the above statements

    contextDocList.executeQueryAsync(Function.createDelegate(this, this.onSuccessDocList), Function.createDelegate(this, this.get_timelines_onFailure));

}
function onSuccessDocList() {
    // Get the collection

    var timelineCollection = this.timelines.getEnumerator();
    var WssId = 0;
    var TermID;
    while (timelineCollection.moveNext()) {
        var timelineRegion = timelineCollection.get_current();
        var Tower = timelineRegion.get_item('Tower');
        Tower = Tower == null ? null : Tower.get_lookupValue();
        var Cat = timelineRegion.get_item('Category');
        Cat = Cat == null ? null : Cat.get_lookupValue();
        var Info = timelineRegion.get_item('Info');
        Info = Info == null ? null : Info.get_lookupValue();
        var Item = {
            ID: timelineRegion.get_item('ID'),
            Tower: Tower,
            Cat: Cat,
            Info: Info
        };
        LoadDocData.push(Item);
    }
}

function CreateItem(){
    var TotalData = LoadDocData.length;
    TotalItration = Math.floor(TotalData / SeleItem );
    updateListItems(initialIter, SeleItem );
   $("#Status").append("<div> Records Update 1 to " + SeleItem +"</div>");

}

function updateListItems(SI, LI) {

    var itemArray = [];
    SuceesData = [];
    var clientContextUpdate = SP.ClientContext.get_current();

    var oList = clientContextUpdate.get_web().get_lists().getByTitle('Documents');



    for (var i = SI; i < LI ; i++) {
        var status = false;
        var catstatus = false;
        var infoStaus = false;
        if ((LoadDocData[i].Cat== null && LoadDocData[i].Info != null) || LoadDocData[i].Tower == null) {
            var da = {
                ID: LoadDocData[i].ID,
                Status: "Failed"
            }
            SuceesData.push(da);
        } else {
            this.oListItem = oList.getItemById(LoadDocData[i].ID);
            if (LoadDocData[i].Tower != null) {
                for (var j = 0; j < Tower.length; j++) {
                    if (LoadDocData[i].Tower == Tower[j].Tower) {
                        var field = oList.get_fields().getByInternalNameOrTitle("Tower1");
                        var taxField = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
                        var taxonomyTower = new SP.Taxonomy.TaxonomyFieldValue();
                        taxonomyTower.set_label(Tower[j].Tower);
                        taxonomyTower.set_termGuid(Tower[j].TowerId);
                        taxonomyTower.set_wssId(Tower[j].WSSID);
                        taxField.setFieldValueByValue(oListItem, taxonomyTower);
                        status = true;
                        break;
                    }
                    status = false;
                }
            }
            if (LoadDocData[i].Cat!= null) {
                for (var j = 0; j < Category.length; j++) {
                    if (LoadDocData[i].Cat == Category[j].Category&&LoadDocData[i].Tower == Category[j].Tower) {
                        var field = oList.get_fields().getByInternalNameOrTitle("Category1");
                        var taxFieldCat = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
                        var taxonomyCat = new SP.Taxonomy.TaxonomyFieldValue();
                        taxonomyCat.set_label(Category[j].Category);
                        taxonomyCat.set_termGuid(Category[j].CategoryId);
                        taxonomyCat.set_wssId(Category[j].WSSID);
                        taxFieldCat.setFieldValueByValue(oListItem, taxonomyCat);
                        catstatus = true;
                        break;
                    }
                    catstatus = false;
                }
            } else
                catstatus = true;
            if (LoadDocData[i].Info != null) {
                for (var j = 0; j < info.length; j++) {
                    if (LoadDocData[i].Info == info[j].info && LoadDocData[i].Cat == info[j].Category && LoadDocData[i].Tower == info[j].Tower) {
                        var field = oList.get_fields().getByInternalNameOrTitle("Info1");
                        var taxFieldInfo = clientContextUpdate.castTo(field, SP.Taxonomy.TaxonomyField);
                        var taxonomyInfo = new SP.Taxonomy.TaxonomyFieldValue();
                        taxonomyInfo.set_label(info[j].info);
                        taxonomyInfo.set_termGuid(info[j].infoId);
                        taxonomyInfo.set_wssId(info[j].WSSID);
                        taxFieldInfo.setFieldValueByValue(oListItem, taxonomyInfo);
                        infoStaus = true;
                        break;
                    }
                    infoStaus = false;
                }
            } else
                infoStaus = true;
            if (catstatus && infoStaus && status) {
                var da = {
                    ID: LoadDocData[i].ID,
                    Status: "Success..."
                }
                SuceesData.push(da);
                oListItem.update();
                itemArray[i] = oListItem;
                clientContextUpdate.load(itemArray[i]);
            } else {
                var da = {
                    ID: LoadDocData[i].ID,
                    Status: "Failed...."
                }
                SuceesData.push(da);
            }
        }
        initialIter++;
    }

    clientContextUpdate.executeQueryAsync(onQuerySucceeded, onQueryFailed);

}



function onQuerySucceeded() {
    //alert('Items Updated');
    CreateListItems()
}



function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

}

function CreateListItems() {

    var itemArray = [];

    var clientContextCreate = SP.ClientContext.get_current();

    var oListC = clientContextCreate.get_web().get_lists().getByTitle('Log Documents');
    var itemCreateInfo;
    for (var i = 0; i < SuceesData.length ; i++) {

        var itemCreateInfo = new SP.ListItemCreationInformation();
        this.oListItemC = oListC.addItem(itemCreateInfo);
        oListItemC.set_item('Title', SuceesData[i].Status);
        oListItemC.set_item('ReferenceID', SuceesData[i].ID);
        oListItemC.update();
        itemArray[i] = oListItemC;
        clientContextCreate.load(itemArray[i]);
    }
    clientContextCreate.executeQueryAsync(onQuerySucceededCreate, onQueryFailed);

}



function onQuerySucceededCreate() {

    if (initialIter < (LoadDocData.length - SeleItem )){
          $("#Status").append("<div> Records Update " + initialIter+" to " + (initialIter+SeleItem) +"</div>");
           updateListItems(initialIter, (initialIter + SeleItem ));
        }
    else if(initialIter <LoadDocData.length){
             $("#Status").append("<div> Records Update " + initialIter+" to " + LoadDocData.length +"</div>");
        updateListItems(initialIter, LoadDocData.length);


        }

   // alert('Items Updated');

}

No comments:

Post a Comment