Update Metadata for Taxonomy Field on File Upload

Good Afternoon,

I have followed this link to update MetaData on File Upload documents.

One of the field is DOC_Department and is a Taxonomy field (Managed Metadata). I have been googling to find a way to update the value in this Taxonomy field.

Here is my codes.

fd.control('Documents').$on('filesUploaded', function(itemIds) {
        SetFilename(itemIds, 'TestDocuments')
});            

function SetFilename(itemIds, v) {

    var docLibraryTitle = 'Test Documents';

    var deptValue =
    {
        "Label": 'Finance',
        "TermGuid": '99999999-9999-9999-99999999999999999',
        "WssId": 20
    };
        
    pnp.sp.web.siteUsers.getByLoginName(fd.field('EmployeePerson').value.Key).get()
        .then(function(result) {
            console.log('FileUpload result ', result);
            console.log('FileUpload resultId ', result.Id);
 
        var library = pnp.sp.web.lists.getByTitle(docLibraryTitle);
        //go through each uploaded Item Id and set field values
        library.getListItemEntityTypeFullName().then(function(entityTypeFullName){

            var batch = pnp.sp.web.createBatch();
			
            for(var i = 0; i < itemIds.length; i++){
                console.log("itemIds ");
                console.log(itemIds);
                //specify which fields to update and how - FileLeafRef: fd.field('Title').value
                console.log('library.items ', library.items.getById(itemIds[i]));
        library.items.getById(itemIds[i]).inBatch(batch).update({
	FileLeafRef: v + '_' + new Date().toISOString().split('.')[0].replace(/[^\d]/gi,'') + '_(' + i + ')',
                    DOC_x0020_OwnerId: result.Id,   
                    DOC_x0020_Department: deptValue,
	DOC_x0020_Category: v
                }, "*", entityTypeFullName);
                
            }   // for
            
            batch.execute().then(function(){
                fd.control(v).refresh();
            });
            
        }); // library
        
    }); // pnp
}

All the fields updated successfully except for the Department Taxonomy field. There's no error showing in the console log so I don't think it's the codes. I found a Github link that mentioned it may be a bug but I am not sure how to fix it.

How can I update Taxonomy field through batch update?

Thank you so much in advance for your help with this!

Hello @COR6603,

The code below works for my Taxonomy field. Please try it out.

Also double check that you are using the valid field internal name.

var listOrLibrary = 'SPDataTable1';
var docLibraryTitle = 'Documents';

fd.spRendered(function() {
    fd.control(listOrLibrary).$on('filesUploaded',
        function(itemIds) {
            //get document library by Title
            var library = pnp.sp.web.lists.getByTitle(docLibraryTitle);
            //go through each uploaded Item Id and set field values
            library.getListItemEntityTypeFullName().then(function(entityTypeFullName){

                var batch = pnp.sp.web.createBatch();

                for(var i = 0; i < itemIds.length; i++){
                    //specify which fields to update and how
                    library.items.getById(itemIds[i]).inBatch(batch).update({
                        Title: fd.field('Title').value,
                        MetaData: {
                            __metadata: { type: 'SP.Taxonomy.TaxonomyFieldValue' },
                            TermGuid: '51c392e4-bc63-4a19-9ca3-7e77821e8c8c',
                            WssId: -1
                        }
                    }, "*", entityTypeFullName);
                }

                batch.execute().then(function(){
                    fd.control(listOrLibrary).refresh();
                });
            });
        });
});

Thank you Margarita! I was able to create a test list and got it to work with the codes you provided.

image

Then I added a custom content type created from my organization and default to it. That seems to break the metadata update. There was no error in the console log either.

image

At least now I know the root cause but still a mystery as to why it won't work. Any thoughts?

Much Appreciated for all your help!

Hello @COR6603,

I couldn't reproduce the issue. The MetaData column is updated for a default and custom content types with no issues.

You can try out updating taxonomy field using this code:

const list = sp.web.lists.getByTitle('Documents');

list.items.getById(192).validateUpdateListItem([
    { FieldName: 'MetaData', FieldValue: 'test|51c392e4-bc63-4a19-9ca3-7e77821e8c8c;' }
]).then(console.log);

You can test it from the console and debug it if it doesn't work. Find out how to debug the code here:

I will try it out. Thank you very very much for your help!

1 Like