2

I have a large, complicated script that copies selected documents from Library A to Library B when a user clicks a button. At the end of the script (as a final step), I'm calling the three functions below to update the metadata on the document to indicate it's been copied successfully. The field being updated is a "Single line of text" column called 'Moved' and I just want to put the value "Yes" in it. Other workflows & formatting scripts then fire off that value as a condition.

The issue I'm seeing is that when a user moves multiple files, the value isn't updated 100% of the time although the document gets copied successfully. About half the time, some of the documents are updated and the rest aren't. If you run the script a second time (by selecting the files & clicking the button again), the field gets updated as expected. The other half of the time, all documents are updated successfully. It's very frustrating, both to me and the users.

I'm currently troubleshooting this issue and need to know if there is a problem with my code below. Since the documents are being copied, the issue seems to be at this point in the script.

function updateFile(itemUrl) {    
 getFileWithProperties(itemUrl,
  function(file){
     var listItem = file.get_listItemAllFields();
     var itemProperties = {'Moved': 'Yes'};
     //update list item
     updateListItem(listItem, itemProperties,
       function () {
         console.log('Item updated succesfully');
       }, 
       function(sender,args){
         console.log(args.get_message());
       }
      );
  },
  function(sender,args){
      console.log(args.get_message());
  });
}

// Get the file based on item url
function getFileWithProperties(url,success,error) {

   var ctx = SP.ClientContext.get_current(); 
   var file = ctx.get_web().getFileByServerRelativeUrl(url);
   ctx.load(file,'ListItemAllFields'); 

   ctx.executeQueryAsync(
      function () {
         success(file);
      }, 
      error
   );
}

// Update the item properties
function updateListItem(listItem,properties, success,error) 
{
   var ctx = listItem.get_context();
   for(var propName in properties) {
       listItem.set_item(propName, properties[propName]) 
   }
   listItem.update();
   ctx.executeQueryAsync(
       function () {
         success();
       }, 
       error
   );
}

Normally, I prefer to figure these kind of things out myself but this time I'm up against a wall both mentally and project-wise. Any help is greatly appreciated.

3

The specified code looks Okay for me. I would suggest to modify updateFile function to include logging (not into browser console), it will allow to simplify the troubleshooting of any errors occurred during updating File properties.

How to log errors into ULS from the client-side

SharePoint JavaScript Library supports logging errors into ULS. The following function allows to log errors into ULS by consuming Diagnostics Web Service:

ULSOnError(msg, url, line)

Modified example

function updateFile(itemUrl) {    
 getFileWithProperties(itemUrl,
  function(file){
     var listItem = file.get_listItemAllFields();
     var itemProperties = {'Moved': 'Yes'};
     //update list item
     updateListItem(listItem, itemProperties,
       function () {
         console.log('Item updated succesfully');
       }, 
       function(sender,args) {
          logError(args.get_message(),itemUrl);
       }
      );
  },
  function(sender,args) {
      logError(args.get_message(),itemUrl);
  });
}

function logError(message,url)
{
    ULS.enable=true;
    ULSOnError(message, url, 'Error occurred while updating File');
}

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.