Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The example demonstrates how to move list item into folder via SharePoint JSOM API
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());
}
@EA12

This comment has been minimized.

Copy link

commented May 8, 2019

Hi Vadim,

nice Peace of Code. Well done.
You can also simply use
file = item.get_file();
to get the file object.

I have not done a benchmark to test which one is faster but less Code is Always nice.

Greetings from Germany, Ronny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.