Change Libary Items values with Javascript

Hey There,

So the idea is, that the user selects items in a list/libary and then clicks a button("add to parent"), JavaScript then populates the "parent_id" field with the ID of the currently opend item.

I got the Button, but i couldnt find anything on how to change the values.

click: function() { var itemIds = fd.control('SPDataTable2').selectedItems); console.log(itemIds) ;}}

Greetings from Germany
Nico

Hello @nI3o.

You need to use PnPjs to update items in another list. Please see the code example below.

Note that you need to add Id after the internal name of the lookup field.

For more information, please see PnPjs documentation on working with SharePoint items.

var parentItemId = fd.itemId;

var table =fd.control('SPDataTable0').selectedItems;

if(table.length != 0){
    for (var i = 0; i < table.length; i++){
        var childItemId = table[i].ID;

        sp.web.lists.getByTitle("ChildListName").items.getById(childItemId).update({

            LookupFieldNameId: parentItemId 
            
        }).then(function() {

            console.log("Updated!");

            });

    }
}

Hey @mnikitina

thanks alot, works perfectly and the docu helps alot.

One more thing :slight_smile:
Whats the syntax for deleting the id value, couldnt find it

@nI3o,

Do you mean clearing the lookup value in the child list?

If so, the code will be the following:

var childItemId = table[i].ID;
        
sp.web.lists.getByTitle("ChildListName").items.getById(childItemId).update({
        
           LookupFieldNameId: null

}).then(function() {

            console.log("Updated!");

   });

Could have figured that out by myself :roll_eyes: thanks :slight_smile:

1 Like