简体   繁体   中英

How to move list item into folder in Sharepoint online

I created a folder and have some item in this list. So now how can I move list item into folder using JSOM. I know user can drag/drop, but I want when user create a list is always move to folder automatically.

The following example demonstrates how to move a 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()); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM