简体   繁体   English

使用ECMA脚本将选定的列表项复制到其他列表

[英]Copy selected List-Items to an other list with ECMA Script

i need a script that copies all my selected list items to an other (custom) list. 我需要一个脚本,将所有我选择的列表项复制到另一个(自定义)列表。 I found nice solution for documents: 我找到了很好的文档解决方案:

var context = SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);

var _destinationlib = web.get_lists().getByTitle('DestinationLibrary');
context.load(_destinationlib);
var notifyId;
var currentlibid = SP.ListOperation.Selection.getSelectedList();

var currentLib = web.get_lists().getById(currentlibid);

var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
var count = CountDictionary(selectedItems);

for(var i in selectedItems)
{
 alert('Now copying ' + i);
 var currentItem =    currentLib.getItemById(selectedItems[i].id);
 context.load(currentItem);

var File = currentItem.get_file();
context.load(File);

//Excecuting executeQueryAsync to get the loaded values
context.executeQueryAsync
(
function (sender, args) {
if(File != null) {

var _destinationlibUrl =  web.get_serverRelativeUrl() + _destinationlib.get_title() + '/' +  File.get_name();

File.copyTo(_destinationlibUrl, true);
notifyId = SP.UI.Notify.addNotification('Moving file…' + File.get_serverRelativeUrl() + 'to' + _destinationlibUrl, true);

//Excecuting executeQueryAsync to copy the file
context.executeQueryAsync(
function (sender, args) {
SP.UI.Notify.removeNotification(notifyId);

SP.UI.Notify.addNotification('File copied successfully', false);
},
function (sender, args) {
SP.UI.Notify.addNotification('Error copying file', false);
SP.UI.Notify.removeNotification(notifyId);
showError(args.get_message());
});
}
},
function (sender, args) {
alert('Error occured' + args.get_message());
}
);
}

I dont know what i have to change to get it working for normal list items. 我不知道我要更改使其正常列表项工作。 I tried to exchange 我试图交流

var File = currentItem.get_file();

context.load(File);

with

var title = currentItem.get_Title();
context.load(title);

var number = currentItem.get_item('number');
context.load(number);

but it dosnt work. 但它没有用。 It would be great if somebody can give me a hint what i have to do. 如果有人可以给我提示我必须做的事,那就太好了。

many thx 许多

Fabulus bul

It looks like that you took code above from here . 看来您是从此处获取了上面的代码。

Try to be attentively. 努力专心。 This code copies selected files (not list items!) to another document library! 此代码将所选文件(不是列表项!)复制到另一个文档库!

For your needs better try to code your own solution. 为了您的需求,最好尝试编写自己的解决方案。 See SharePoint JavaScript Class Library for details. 有关详细信息,请参见SharePoint JavaScript类库 You can have two possible architectures: 您可以有两种可能的体系结构:

  1. Make all work from JavaScript. 使用JavaScript进行所有工作。 And the first your step will be addItem method of SP.List . 第一步是SP.List的addItem方法
  2. Make processing of selection on client in JavaScript and call your custom server-side component (may be an application page) for items copying (creating copies in new list of already existed items from initial list.). 使用JavaScript在客户端上进行选择处理,并调用自定义服务器端组件(可能是应用程序页面)进行项目复制(在初始列表中已存在项目的新列表中创建副本)。 See this for example. 参见示例。

Also be careful with context.load. 也要注意context.load。 It's recommended to write all next code in context.executeQueryAsync. 建议在context.executeQueryAsync中编写所有下一个代码。 Use Firebug in FF and developer tools in Chrome for debugging your code and to find what is wrong. 使用FF中的Firebug和Chrome中的开发人员工具来调试代码并找出问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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