I am on a SharePoint 2013 cloud setup (but for testing purposes, it is a prem server).

I am customizing an Edit form for a list, so that when a user checks a box the preSave event will copy the file attachment to the Document Library (and hopefully kick off workflows and other things).

After doing some searching, I managed to create a function that should copy the file from the Memo list to the Shared Documents.

function justCopyIt() {
    var fileContentURL = "/_api/SP.AppContextSite(@target)/web/getfilebyserverrelativeurl('/sites/MySite/Lists/Memo/" 
    + $.local.arrFileName + "')/copyto(strnewurl='/sites/MySite/Lists/Shared Documents/" 
    + $.local.arrFileName + "',boverwrite=true)'"

    var executor = new SP.RequestExecutor(appweburl);
          url: fileContentURL,
          method: "POST",
          success: successHandler,
          error: errorHandler

    function successHandler(data) {
        var jsonObject = JSON.parse(data.body);
        console.info("success : " + jsonObject.d.Title );

    function errorHandler(data, errorCode, errorMessage) {
        console.info("error code : " + errorCode + " \n error : " + errorMessage);

When I run the function, I get a error code of "-1002" and text of "FORBIDDEN".

I honestly don't know if I have the copyto portion correct, so that might be the issue. Or it could basically be some sort of permissions issue.

1 Answer 1


I did another search and found a question that was similar to mine labeled: Automatically add attachment to new list item

After playing with the code, I was able to get mine working.

First, create the function to get the current list and current item in order to find the attachment as the file object.

The getUrlParameter("ID") is a short little function that splits out the page URL and retrieves the value for the parameter.

function getQueryStringParameter(paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";

    for (var i = 0; i < params.length; i = i + 1) {
      var singleParam = params[i].split("=");
      console.info("singleParam : " + singleParam);
      if (singleParam[0] == paramToRetrieve)
        return singleParam[1];

Here is the starting point of the copy, which is the function to get the source file.

function justCopyIt() {
        thisClientContext = new SP.ClientContext.get_current();    
        this.web = thisClientContext.get_web();
        this.sourceList = this.web.get_lists().getByTitle("Memo");
        this.currentItem = sourceList.getItemById(getUrlParameter("ID"));

        var attachmentFolder = this.web.getFolderByServerRelativeUrl("/sites/MySite/Lists/Memo/Attachments/" + getUrlParameter("ID") + "/");    
        sourceFile = attachmentFolder.get_files();

        thisClientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onGetFail));

After the source file is loaded, call the executeQueryAsync to run the query. I have a onGetFail function that I handle all failed query calls.

function onQuerySucceeded(sender, args) {
        if (sourceFile != null) {

            destinationlibUrl = "http://myServer/sites/MySite/Shared%20Documents/" + $.local.arrFileName;      
            sourceFile.itemAt(0).copyTo(destinationlibUrl, 1);

            notifyId = SP.UI.Notify.addNotification("Copying file " + sourceFile.itemAt(0).get_serverRelativeUrl() + " to " + destinationlibUrl, true);
                function (sender, args) {
                    SP.UI.Notify.addNotification('File copied successfully', true);          
                function (sender, args) {
                     SP.UI.Notify.addNotification('Error copying file', false);
                     console.error('Error occured: ' + args.get_message());

When the onQuerySucceed function runs, it does the copyTo and adds a on screen notification to let the user know the "Copying File" has started and when it is successful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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