I have a list which each row has at most 1 attachment, and I am trying to copy each attachment from this list to a newly created picture library.
I am able to copy 1 attachment by the following code,
var Files;
var myContext;
function CopyAtt(ItemID) {
try {
myContext = new SP.ClientContext.get_current();
var myWeb = myContext.get_site().get_rootWeb();
var folderPath = 'Lists/test/Attachments/' + ItemID;
var Folder = myWeb.getFolderByServerRelativeUrl(folderPath);
Files = Folder.get_files();
myContext.load(Files);
myContext.executeQueryAsync(Function.createDelegate(
this, ExecuteCopyOnSuccess),
Function.createDelegate(
this, GetLeadsFail));
}
catch (err) {
alert(err.Line);
}
}
function GetLeadsFail(sender, args) {
// Show error message
alert('GetLeadsFail() failed:' + args.get_message());
}
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 = 'PictureLibrary/' + filename;
file.copyTo(newUrl, true);
myContext.executeQueryAsync(null, null);
}
}
$(document).ready(function() {
CopyAtt(3);
}
When I try to call CopyAtt(ItemID)
multiples times from $(document).ready
,
the code is showing errors in console.
Uncaught TypeError: Cannot read property 'collectionHasNotBeenInitialized' of undefined
Uncaught RangeError: Maximum call stack size exceeded
I suspect that it is related to Files but I cannot find any clues, can anyone give me some advice?
I got a solution several days later, the problem is not related to Files, but myContext.executeQueryAsync(null, null);
.
At first I call CopyAtt(ItemID)
multiple times,
for (i=0; i< Items.Length; i++){
CopyAtt(Items[i]);
}
executeQueryAsync()
in CopyAtt(ItemID)
will break the loop and show errors above.
Rather than for loop, recursion can avoid such errors, and each
executeQueryAsync()
will only be executed after one is completed.
function CopyAtt(Items, itemIndex) {
try {
myContext = new SP.ClientContext.get_current();
var myWeb = myContext.get_site().get_rootWeb();
var folderPath = 'Lists/test/Attachments/' + Items[itemIndex];
var Folder = myWeb.getFolderByServerRelativeUrl(folderPath);
Files = Folder.get_files();
myContext.load(Files);
myContext.executeQueryAsync(Function.createDelegate(
this, ExecuteLoadFileSuccess(Items, itemIndex);),
Function.createDelegate(
this, GetLeadsFail));
}
catch (err) {
alert(err.Line);
}
}
function GetLeadsFail(sender, args) {
// Show error message
alert('Request failed - ' + args.get_message());
}
function ExecuteLoadFileSuccess(Items, itemIndex, 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 = 'PictureLibrary/' + filename;
file.copyTo(newUrl, true);
myContext.executeQueryAsync(Function.createDelegate(
this, function(){ExecuteCopyOnSuccess(Items, itemIndex);}),
Function.createDelegate(
this, GetLeadsFail));
}
}
function ExecuteCopyOnSuccess(Items, itemIndex, sender, args) {
//Call CopyAtt() after copy files success.
if (itemIndex <Items.length-1) {
CopyAtt(Items, itemIndex+1);
}
}
$(document).ready(function() {
//save all Items ID in an array.
var Items = [2,3,6,7,8,10];
CopyAtt(Items, 0);
}