2

i got this code, what this does, is that it takes a .csv file, and converts into json(used papaparce.js to convert csv into json) and with that data i create and populate a new document set fields, my question is:

How can i retrieve some managed metadata fields based on the info from the json?

i just don't know how i can get the info i need in the time i need to create this while the document set is being created created.

The piece

If i give the right term with the right term id, with this function the metadata will update okay in the document set. ej:

setTaxField('city','Paris',xyzabcde-c68d-4c6d-b587-48ddaf1aecc9)

I need to get the term id from the label before to pass this data how can i do that?


function setTaxField(field,tag,txGUID){
    var field = list.get_fields().getByInternalNameOrTitle(field);  
    var taxField = ctx.castTo(field, SP.Taxonomy.TaxonomyField);  
    var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();  
    taxonomySingle.set_label(tag);  
    taxonomySingle.set_termGuid(txGUID);  
    taxonomySingle.set_wssId(-1);  
    taxField.setFieldValueByValue(docSetFolderItem, taxonomySingle);
}                       

//I retrive the term tag from the json, and i want to get the term id so i can save the metadata field, how can i do that?

setTaxField('city',data['city'],xyzabcde-c68d-4c6d-b587-48ddaf1aecc9)

The functions i have found that i can use to get the term label data and ID i need

function setTermSet(oTermset, label){

    SP.SOD.loadMultiple(['SP.ClientContext', 'SP.Taxonomy.TaxonomySession'], function(){
       getTermSetByName(oTermset,g_wsaLCID,
         function(termSet){
            console.log(termSet.get_id().toString());
            getATermId(termSet.get_id().toString(), label); 
         },
         function(sender,args){
            console.log(args.get_message());
         });

    });
}


function getTermSetByName(termSetName,lcid,success,error){
   var ctx = SP.ClientContext.get_current();
   var taxonomySession = SP.Taxonomy.TaxonomySession.getTaxonomySession(ctx);
   var termStore = taxonomySession.getDefaultSiteCollectionTermStore();
   var termSets = termStore.getTermSetsByName(termSetName,lcid);
   var termSet = termSets.getByName(termSetName);
   ctx.load(termSet);
   ctx.executeQueryAsync(
     function(){
        success(termSet);
     },
     error);
}


function GetFullTermStringByName(termName, termSetId, inputId) {
    var clientContext = new SP.ClientContext.get_current();
    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(clientContext);
    var termStore = taxSession.getDefaultKeywordsTermStore();
    var termSet = termStore.getTermSet(termSetId);
    var terms = termSet.getAllTerms();
    clientContext.load(terms);
    clientContext.executeQueryAsync(
        function () {
            var e = terms.getEnumerator();
            var targetTerm;
            while (e.moveNext()) {
                term = e.get_current();
                if(term.get_name() == termName) {
                    var element = document.getElementById(inputId);
                    if(element.value == '')
                        element.value = termName + '|' + term.get_id();
                    else
                        element.value = element.value + ';' + termName + '|' + term.get_id();
                    break
                }
            }
        },
        onFail
    );
}

function onFail() {
    //FAILED
}

Complete code

<script type="text/javascript" src="/_catalogs/masterpage/js/jquery-1.12.4.min.js"></script>
<script type="text/javascript" src="/_catalogs/masterpage/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="/_layouts/15/MicrosoftAjax.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="/_layouts/15/SP.DocumentManagement.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.taxonomy.js"></script>
<script type="text/javascript" src="/_catalogs/masterpage/js/papaparse.min.js"></script>

<input type="file" id="csvfile" />
<script type="text/javascript">

 $( document ).ready(function() {
  var   listTitle = 'theList',
    Url = _spPageContextInfo.webAbsoluteUrl,
    loadFile =  document.querySelector("#viewfile"),    



    function createItems(data){
    SP.SOD.executeFunc('sp.documentmanagement.js', 'SP.DocumentSet.DocumentSet', function() {       
        for (var i = 0; i < data.length-1; i++) {
            createDocumentSet(Url,listTitle,data[i]['NombreDocSet'],data[i],success,fail);              
            updateProgress(counter, data.length-1);
            counter++
        }

    });
}


function createDocumentSet(siteUrl,listTitle,docSetName,data,success,fail) {
    var ctx = new SP.ClientContext(siteUrl);
    var web = ctx.get_web();
    var list = web.get_lists().getByTitle(listTitle);
    ctx.load(list);

    var parentFolder = list.get_rootFolder();
    ctx.load(parentFolder);

    var docSetContentTypeID = "0x0120D520";
    var docSetContentType = ctx.get_site().get_rootWeb().get_contentTypes().getById(docSetContentTypeID);
    ctx.load(docSetContentType);

    ctx.executeQueryAsync(function (){ 


        SP.DocumentSet.DocumentSet.create(ctx, parentFolder, docSetName, docSetContentType.get_id());      

        var docSetFolder = web.getFolderByServerRelativeUrl(parentFolder.get_serverRelativeUrl() + '/' + docSetName);
        var docSetFolderItem = docSetFolder.get_listItemAllFields();            

        function setTaxField(field,tag,txGUID){
            var field = list.get_fields().getByInternalNameOrTitle(field);  
            var taxField = ctx.castTo(field, SP.Taxonomy.TaxonomyField);  
            var taxonomySingle = new SP.Taxonomy.TaxonomyFieldValue();  
            taxonomySingle.set_label(tag);  
            taxonomySingle.set_termGuid(txGUID);  
            taxonomySingle.set_wssId(-1);  
            taxField.setFieldValueByValue(docSetFolderItem, taxonomySingle);
        }                       

        //I retrive the term tag from the json, and i want to get the term id so i can save the metadata field, how can i do that?

        setTaxField('city',data['city'],xyzabcde-c68d-4c6d-b587-48ddaf1aecc9)


        //Document set fields 
        docSetFolderItem.set_item('field1', data['field1']);        
        docSetFolderItem.set_item('field2', data['field2']);
        docSetFolderItem.set_item('field3', data['field3']),            
        docSetFolderItem.set_item('field4', data['field4']);
        docSetFolderItem.set_item('field5', data['field5']);
        docSetFolderItem.set_item('field6', data['field6']);                


        //update document set element
        docSetFolderItem.update();
        ctx.load(docSetFolderItem);        

        ctx.executeQueryAsync(function () {
            console.log(docSetFolderItem);
            console.log(docSetFolder);
            counter++
            success(docSetFolderItem, data);
        },fail);

    }, 
    fail);
}

function getJson(){
    //get csv file
    var file = document.getElementById('csvfile').files[0];   
    Papa.parse(file, {
        header: true,
        complete: function(results) {   
            //create file from data obtained from csv
            createItems(results.data)          
        }
    });
}

var success = function(item, data) {
    //counter++;
    console.log(data);    
};

var fail = function (err, args, data) {
    console.log(args.get_message())
};  


function init() {
    loadFile.addEventListener('click',function (){
        getJson();

    }); 

}

init();
});
 </ script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.