0

I have a document library and I would like to update the metadata(columns) of a document using JSOM. I thought the following would work but it works just on custom list.

 var ctx = new SP.ClientContext.get_current();
                            var customLibrary = ctx.get_web().get_lists().getByTitle('Shared Documents');
                            var listItem = customList.getItemById(itemID);

                            /*Set the value and update*/
                            listItem.set_item('Project_0020_Number', prjNum);
                            listItem.update();

                            ctx.executeQueryAsync(
                            function(){ 
                                                                            /*Need to change this to show on the page*/
                                                                            alert('Updated'); 
                                            }, 

                                            function(sender, args){ alert('Error: ' + args.get_message()); });
                                            }

                                            function onQueryFailed(sender, args) {
                                                            alert('Request failed. '+args.get_message() + '\n' + args.get_stackTrace());
1

Looks like you have not posted the entire code. However, with the code that you have posted, there's some issue with the parenthesis closing. You have not closed ctx.executeQueryAsync( correctly.

Also, by default, the title of Shared Documents document library is 'Documents'. Have modified the code now.

Below is a sample working code modified from your code:

var ctx = new SP.ClientContext.get_current();
var customList = ctx.get_web().get_lists().getByTitle('Documents');
var listItem = customList.getItemById(itemID);

/*Set the value and update*/
listItem.set_item('Project_0020_Number', prjNum);
listItem.update();

ctx.executeQueryAsync(function(){
    /*Need to change this to show on the page*/
    alert('Updated'); 
},function(sender, args){
    alert('Request failed. '+args.get_message() + '\n' + args.get_stackTrace());
});
  • My apologies. I meant to say "Custom List" and not Library. The code above works on a list but not a document library. I will make the change to the original question. – JustMe Oct 5 '17 at 10:57
  • I will still try your code though on my library – JustMe Oct 5 '17 at 11:00
  • Here's the error I get "Request failed. List 'Shared Documents' does not exist at site with URL 'isogunro.com/nrm. undefined" – JustMe Oct 5 '17 at 11:14
  • change the line as ctx.get_web().get_lists().getByTitle('Documents'); and check – Gautam Sheth Oct 5 '17 at 11:15
  • Any luck after you modified the code ? – Gautam Sheth Oct 6 '17 at 5:07
0

I was unable to find a solution to update a document library using JSOM so I went searching for another solution that can be found here using REST API and jquery.

After the document was uploaded, I was able to modify the code in the link and update the metadata like so

"var body = String.format("{{'__metadata':{{'type':'{0}'}},'FileLeafRef':'{1}','Title':'{2}','<internal column name>':'{3}'}}",
itemMetadata.type, newName, newName,<column data>);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.