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());

2 Answers 2

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());
});
6
  • 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. Commented Oct 5, 2017 at 10:57
  • I will still try your code though on my library Commented Oct 5, 2017 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" Commented Oct 5, 2017 at 11:14
  • change the line as ctx.get_web().get_lists().getByTitle('Documents'); and check Commented Oct 5, 2017 at 11:15
  • Any luck after you modified the code ? Commented Oct 6, 2017 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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.