The requirement is as follows:
1. We have a SP list. All items in the list have mulitple attachments.
2. On click of a button, All the attachments of all items should be copied to a document library
This is to be done from client side only. No server code is acceptable. We have tried following things:
1. USing Asynch call
context = new SP.ClientContext(“my site name”);
this.oWebsite = context.get_web();
var lists = oWebsite.get_lists();
var list = lists.getByTitle(‘my list name’);
context.load(oWebsite);
var folderPath = ‘Lists/<my list name>/Attachments/’ + folderId;
var Folder = oWebsite.getFolderByServerRelativeUrl(folderPath);
context.load(Folder);
Files = Folder.get_files();
context.load(Files);
context.executeQueryAsync(Function.createDelegate(this, this.ExecuteCopyOnSuccess), Function.createDelegate(this, this.ExecuteCopyOnFailure));
function ExecuteCopyOnSuccess(sender, args) {
for(var p=0;p<this.Files.get_count();p++)
{
var file = Files.itemAt(p);
var filename = file.get_name();
if (filename != null) {
var newUrl = ‘document library url’;
file.copyTo(newUrl, true);
context.executeQueryAsync(null,null);
}
}
}
In this case, Files.get_count() throws error – The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
2. Using copy.asmx service
It copies files to document library but some files are blank and are having size 0.
Any pointers for above issue ..
Thanks in Advance …!
Hi Mihir! I am also searching for the exact same functionality…to copy attachments from one list item to another….! Have you found a solution yet..Because so far I could not find an answer. Please let me know if you have found one 🙂 Thank You!
Hello, Thanks for the solution. But is it possible to copy attachments to another list item. Means, i want to copy the attachments from one list item to another list item.
use this SP.ListOperation.Selection.getSelectedItems(); this will give you all selected items and then loop through selected items and get the id for each, pass ids to our function and you are done. Make sure you use the listview.
Multiple attachment with multiple items –
On one page List items will be displayed with cehck box . user wil select one or more check box.
And on button click – we are checkign selected items and attachments for that item. and move them to document lib ..
For one item with one attachment it works .. fails for other scenarios .. again thanks for your reply and help .. !
Â
Multiple attachment with one item or multiple items?