1

I am trying to upload the email attachment to SharePoint list item using JavaScript. But I landed up on the following error on the console.

Cannot rename "_120" to "120": destination already exists.

Below is the code for the reference which I use for uploading attachment to SharePoint.

function processUpload(fileInput, listTitle, itemId,success,error) {
    var reader = new FileReader();
    reader.onload = function (result) {
        var fileContent = new Uint8Array(result.target.result);
        performAttachmentUpload(listTitle, fileInput.name, itemId, fileContent,success,error);
    };
    reader.readAsArrayBuffer(fileInput);
}


function performAttachmentUpload(listTitle, fileName, itemId, fileContent,success,error) {

    ensureAttachmentFolder(listTitle,itemId, 
       function(folder){
           var attachmentFolderUrl = folder.get_serverRelativeUrl();
           uploadFile(attachmentFolderUrl,fileName,fileContent,success,error);
       },
       error);
}


function ensureAttachmentFolder(listTitle,itemId, success,error)
{
      var ctx = SP.ClientContext.get_current();
      var web = ctx.get_web();
      var list = web.get_lists().getByTitle(listTitle);
      ctx.load(list,'RootFolder');
      var item = list.getItemById(itemId);
      ctx.load(item);
      ctx.executeQueryAsync(
        function() {
            var attachmentsFolder;
            if(!item.get_fieldValues()['Attachments']) { /* Attachments folder exists? */
               var attachmentRootFolderUrl = String.format('{0}/Attachments',list.get_rootFolder().get_serverRelativeUrl()); 
               var attachmentsRootFolder = ctx.get_web().getFolderByServerRelativeUrl(attachmentRootFolderUrl);
               //Note: Here is a tricky part. 
               //Since SharePoint prevents the creation of folder with name that corresponds to item id, we are going to:   
               //1)create a folder with name in the following format '_<itemid>'
               //2)rename a folder from '_<itemid>'' into '<itemid>'
               //This allow to bypass the limitation of creating attachment folders
               attachmentsFolder = attachmentsRootFolder.get_folders().add('_' + itemId);
               attachmentsFolder.moveTo(attachmentRootFolderUrl + '/' + itemId);
               ctx.load(attachmentsFolder);
            }
            else {
               var attachmentFolderUrl = String.format('{0}/Attachments/{1}',list.get_rootFolder().get_serverRelativeUrl(),itemId); 
               attachmentsFolder = ctx.get_web().getFolderByServerRelativeUrl(attachmentFolderUrl);
               ctx.load(attachmentsFolder);
            }         
            ctx.executeQueryAsync(
                 function() {
                     success(attachmentsFolder); 
                 },
                 error);
        },
        error);
}


function uploadFile(folderUrl,fileName,fileContent,success,error)
{
     var ctx = SP.ClientContext.get_current();
     var folder = ctx.get_web().getFolderByServerRelativeUrl(folderUrl);
     var encContent = new SP.Base64EncodedByteArray(); 
     for (var b = 0; b < fileContent.length; b++) {
         encContent.append(fileContent[b]);
     }
     var createInfo = new SP.FileCreationInformation();
     createInfo.set_content(encContent); 
     createInfo.set_url(fileName);
     folder.get_files().add(createInfo);
     ctx.executeQueryAsync(success,error);
}

I pasted the attachments link the File Explorer, I am able to open the "_120" folder but not able to view them or delete it.

What could be the solution for this.

EDIT 1:-

Well I think this can be the reason behind this error. I just tried to open Attachment Folder of a list item on Windows Explorer and deleted the attachment from the folder of the list item ID i.e. 120.

After the deletion, when I tried to upload the attachment again to same list item it gave me the same error.

Is there any way that I can delete the attachment through SharePoint itself rather than opening the attachment folder in Windows Explorer.

Your Answer

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

Browse other questions tagged or ask your own question.