Created
March 30, 2016 19:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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