Link to home
Start Free TrialLog in
Avatar of ebertsys
ebertsysFlag for Germany

asked on

Want to copy attachments from one list to a other

I have Sharepoint 2013 with workflows2010
problem:
i have a list with attchments..i want to copie some items into a new list via workflow
i can't select the attchment field in Sharepoint Designer.
How to copie (or better a link) to the orign attachments into the new list?
Avatar of Piotr Strycharz
Piotr Strycharz
Flag of Poland image

Well, you do not give too much information about why and how you want to achieve your goal. Do you need to copy list item, attachments or both? How to choose a destination? SharePoint 2013 or 2010 (or destination is different)? Is workflow definitely a good choice? Maybe just ribbon action is enough.

Anyway. 'Attachments' field is actually a boolean field - true if list item has attachments, false - if not. You will not have much benefit of it. I would use some JavaScript action:

var url = _spPageContextInfo.webAbsoluteUrl +
  "/_api/web/lists/getbyid('"+ _spPageContextInfo.pageListId +
  "')/items(" +
  SP.ListOperation.Selection.getSelectedItems()[0].id +")/AttachmentFiles";
$.ajax({
  url: url,
  method: "GET",
  headers: { "Accept": "application/json; odata=verbose" },
  success: function (data) {
    var html = [];
    if (data.d.results.length > 0) {  
      $.each(data.d.results, function () {  
        html.push("<div><a href='" + this.ServerRelativeUrl + "'>" + this.FileName + "</a></div>");
      });  
    }
    updateListItem(html.join(''));
  },
  error: function () { alert("failure"); }
});

function updateListItem(html) {
$.ajax({
  url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('DestinationListTitle')/items([provide target list item ID])",
  type: "POST",
  data: JSON.stringify({  __metadata: { type: "SP.Data.DestinationListNameListItem" }, Link: html }),  // 'Link' is field name to be updated
  headers:  {
    "Accept": "application/json;odata=verbose",
    "Content-Type": "application/json;odata=verbose",
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
    "IF-MATCH": "*",
    "X-HTTP-Method": "MERGE"
  },
  success: function()  { alert('Done') },  
  error: function() { alert('Error'); }
});  
}

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.