Skip to content

Instantly share code, notes, and snippets.

@ahsanranjha
Forked from vgrem/SP.ListItem.moveTo.js
Created July 4, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahsanranjha/4b88c0931d2d188b7532890377ec6a39 to your computer and use it in GitHub Desktop.
Save ahsanranjha/4b88c0931d2d188b7532890377ec6a39 to your computer and use it in GitHub Desktop.
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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment