How to move list item into folder in Sharepoint online

妖精的绣舞 提交于 2019-12-13 05:25:11

问题


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.


回答1:


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()); 
}


来源:https://stackoverflow.com/questions/36255335/how-to-move-list-item-into-folder-in-sharepoint-online

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!