Skip to main content

Search

Need help with content types in http request

Answered

Comments

2 comments

  • Official comment
    skybow Support

    Hi Christoph,

    This example demonstrates how to copy list items attachments to a new item from another SharePoint list.
    In order to archive this, I customized the display form of List1, and added a new button with the following actions configuration:


    1) Variables:

    SourceListTitle - title of source list
    SourceItemID - ID of source list item
    TargetListTitle - the title of the target list, where new item will be added
    TargetListItemID - ID of new item, that should be attachments copied to. Initially value is empty, it will be set after the item is created

    2) Add List item

    In this step, we add a new item to List2


    3) Set variable
    It updates the value for TargetListItemID variable, it will be a newly created item ID from the previous step.


    4) Send HTTPRequest - GetAttachments
    Load a list of attachments for the item.

    URL: [[@Web.ServerRelativeUrl]]/_api/web/lists/getbytitle('[[@Variables.SourceListTitle]]')/items([[@Variables.SourceItemID]])/AttachmentFiles

    Headers:

    Accept: application/json;odata=verbose

     The resonse will have the following JSON format :
    {
    d : {
    results: [{
    FileName: "file1.txt",
    ServerRelativeUrl: "/sites/site1/test-solution2/Lists/List1/Attachments/1/file1.txt"
    },
    {
    FileName: "file2.txt",
    ServerRelativeUrl: "/sites/site1/test-solution2/Lists/List1/Attachments/1/file2.txt"
    }]
    }
    }


    5) Next steps I go through all attachments and copy them one by one synchronously

    Script:

    //Read the attachment file content
    function downloadfile(downloadUrl) {
       return new Promise(resolve => {
           var xhr = new XMLHttpRequest();
           xhr.open('GET', downloadUrl, true);
           xhr.setRequestHeader('binaryStringResponseBody', 'true');
           xhr.responseType = 'arraybuffer';
           xhr.onload = function (e) {
                if (this.status == 200) {
                   var arrayBuffer = this.response;
                   resolve(arrayBuffer);
               }
           };
           xhr.send();
       });
    }

    //Uploads the attachment file to list item
    function uploadAttachment(uploadUrl, arrayBuffer, fileName) {
       return new Promise((resolve, reject) => {
           jQuery.ajax({
               url: uploadUrl,
               type: "POST",
               data: arrayBuffer,
               processData: false,
               contentType: "application/json;odata=verbose",
               headers: {
                   "accept": "application/json;odata=verbose",
                   "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
               }
           }).done(postData => {
               console.log("Uploaded file attachment: " + fileName);
               resolve();
           }).fail(function (jqXHR, errorText) {
               console.error(errorText);
               reject("File attachment is not uploaded: " + fileName);
           });
       });
    }

    //Copy attachment from source item to the newly created item of target list
    function copyAttachment(fileName) {
       return downloadfile([[@Web.ServerRelativeUrl]]+ "/_api/web/lists/getbytitle('" + [[@Variables.SourceListTitle]]+ "')/items(" + [[@Variables.SourceItemID]]+ ")/AttachmentFiles/getbyfilename('" + fileName + "')/$value')")
           .then(arrayBuffer => {
               return uploadAttachment([[@Web.ServerRelativeUrl]]+ "/_api/web/lists/GetByTitle('" + [[@Variables.TargetListTitle]]+ "')/items(" + [[@Variables.TargetListItemID]]+ ")/AttachmentFiles/add(FileName='" + fileName + "')", arrayBuffer, fileName)
           });
    }

    //A list of file names of item attachments
    var attachmentFileNames = [[@Actions.Send_HTTP_request_GetAttachments.ResponseBody]].d.results.map(attachment => attachment.FileName);

    //Copies all attachments synchronously one by one
    function uploadNextAttachment() {
       if (attachmentFileNames.length == 0) {
           return Promise.resolve();
       }
       else {
           //Get first attachment file name and remove it from a list
           var fileName = attachmentFileNames.shift();
           return copyAttachment(fileName).then(() => {
               return uploadNextAttachment();
           });
       }
    }

    return uploadNextAttachment();

     

    Best regards,

    YuriyStetsyuk

  • Christoph Käferböck

    Hi Yuriy,

    I implemented your solution and it worked from the first moment on as expected. Many thanks to you!

    Christoph

    0

Please sign in to leave a comment.