Currently, I am facing an issue while attempting to transfer a list item from one folder to another. Despite utilizing a code that I discovered (the link to the source of the code can be found below), I keep encountering an error message stating "Value does not fall within the expected range." which perplexes me. Can someone please shed some light on this problem for me? Thank you!
var listTitle = "Requests"; //list title
var itemId = 1; //list item id
var targetFolderUrl = "/Lists/Requests/Archive"; //target folder server relative url
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var item = list.getItemById(itemId);
ctx.load(item,['FileRef','FileDirRef']);
ctx.executeQueryAsync(
function(){
var fileUrl = item.get_item('FileRef');
var file = ctx.get_web().getFileByServerRelativeUrl(fileUrl);
var targetfileUrl = fileUrl.replace(item.get_item('FileDirRef'),targetFolderUrl);
file.moveTo(targetfileUrl, SP.MoveOperations.overwrite);
ctx.executeQueryAsync(
function(){
console.log('List item has been moved');
},
logError
)
},
logError);
function logError(sender,args){
console.log(args.get_message());
}
This is the actual code I have implemented.
var targetFolderUrl = "/Lists/" + COMM.PROPERTY.SUSAR.LIST + "/ProductID_64"; //target folder server relative url
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(COMM.PROPERTY.SUSAR.LIST);
var item = list.getItemById(66);
ctx.load(item, ['FileRef', 'FileDirRef']);
ctx.executeQueryAsync(
function () {
var fileUrl = item.get_item('FileRef');
var file = ctx.get_web().getFileByServerRelativeUrl(fileUrl);
var targetfileUrl = fileUrl.replace(item.get_item('FileDirRef'), targetFolderUrl);
file.moveTo(targetfileUrl, SP.MoveOperations.overwrite);
ctx.executeQueryAsync(
function () {
console.log('List item has been moved');
},
logError
)
},
logError);
function logError(sender, args) {
console.log(args.get_message());
}
Guidance on moving list items into folders in SharePoint online