Skip to content

Instantly share code, notes, and snippets.

@vgrem
Created March 30, 2016 19:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vgrem/36709eaaf1c153ae017d258d4455d87b to your computer and use it in GitHub Desktop.
Save vgrem/36709eaaf1c153ae017d258d4455d87b 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());
}
@EA12
Copy link

EA12 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