Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否使用j query、JavaScript或rest API将SharePoint列表项从一个列表复制到另一个带有附件的列表?_Javascript_Jquery_Sharepoint 2013_Office365api_Sharepoint List - Fatal编程技术网

是否使用j query、JavaScript或rest API将SharePoint列表项从一个列表复制到另一个带有附件的列表?

是否使用j query、JavaScript或rest API将SharePoint列表项从一个列表复制到另一个带有附件的列表?,javascript,jquery,sharepoint-2013,office365api,sharepoint-list,Javascript,Jquery,Sharepoint 2013,Office365api,Sharepoint List,有没有JavaScript或jQuery方法可以将带有附件的SharePoint列表从一个网站集复制到另一个网站集 我使用Excel导出和导入,但它不会带回附件 非常感谢您的帮助。您可以使用以下JavaScript代码将列表项从一个列表克隆到另一个列表: <button type="button" id="buttoninsert" onclick="insert()">Insert</button> <script src="https://ajax.goo

有没有JavaScript或jQuery方法可以将带有附件的SharePoint列表从一个网站集复制到另一个网站集

我使用Excel导出和导入,但它不会带回附件


非常感谢您的帮助。

您可以使用以下JavaScript代码将列表项从一个列表克隆到另一个列表:

<button type="button" id="buttoninsert" onclick="insert()">Insert</button>  

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var sourceListTitle = "Source List Title";
var destListTitle = "Destination List Title";

function insert() {
    //copy all items in source list to destination list
    getSourceListItems().then(copyItems);
}

function getSourceListItems() {
    var deferred = $.Deferred();
    var ctx = SP.ClientContext.get_current();

    this.sourceList = ctx.get_web().get_lists().getByTitle(sourceListTitle);
    this.sourceListFieldCollection = sourceList.get_fields();
    this.destList = ctx.get_web().get_lists().getByTitle(destListTitle);

    ctx.load(sourceList);
    ctx.load(sourceListFieldCollection);
    ctx.load(destList);

    var items = sourceList.getItems(SP.CamlQuery.createAllItemsQuery());
    ctx.load(items);

    ctx.executeQueryAsync(
        function () { deferred.resolve(items); },
        function (sender, args) { deferred.reject(sender, args); }
    );
    return deferred.promise();
}

function logError(sender, args) {
    console.log('An error occured: ' + args.get_message());
}

function logSuccess(sender, args) {
    console.log('Copied items.');
}

function copyItems(items) {
    $.when.apply(items.get_data().forEach(function (sourceItem) { cloneItem(sourceItem); }))
                                 .then(logSuccess, logError);
}

function cloneItem(sourceItem) {
    var deferred = $.Deferred();
    var ctx = sourceItem.get_context();

    var itemCreateInfo = new SP.ListItemCreationInformation();
    var targetItem = destList.addItem(itemCreateInfo);

    var fieldEnumerator = sourceListFieldCollection.getEnumerator();
    while (fieldEnumerator.moveNext()) {
        var oField = fieldEnumerator.get_current();
        //exclude certain fields
        if (!oField.get_readOnlyField() && 
            oField.get_internalName() !== "Attachments" && 
            !oField.get_hidden() && 
            oField.get_internalName() !== "ContentType") 
        {
            var sourceFieldVal = sourceItem.get_item(oField.get_internalName());
            if (sourceFieldVal != null) {
                targetItem.set_item(oField.get_internalName(), sourceFieldVal);
            }
        }
    }

    targetItem.update();
    ctx.load(targetItem);

    ctx.executeQueryAsync(
        function () { deferred.resolve(); },
        function (sender, args) { deferred.reject(sender, args);
    });
    return deferred.promise();
}
</script>
插入
var sourceListTitle=“源列表标题”;
var destListTitle=“目的地列表标题”;
函数插入(){
//将源列表中的所有项目复制到目标列表
getSourceListItems()。然后(copyItems);
}
函数getSourceListItems(){
var deferred=$.deferred();
var ctx=SP.ClientContext.get_current();
this.sourceList=ctx.get_web().get_lists().getByTitle(sourceListTitle);
this.sourceListFieldCollection=sourceList.get_fields();
this.destList=ctx.get_web().get_lists().getByTitle(destListTitle);
ctx.load(源列表);
ctx.load(sourceListFieldCollection);
ctx.load(目的地列表);
var items=sourceList.getItems(SP.CamlQuery.createAllItemsQuery());
ctx.荷载(项目);
ctx.executeQueryAsync(
函数(){deferred.resolve(items);},
函数(发送方,参数){延迟。拒绝(发送方,参数);}
);
延迟返回。承诺();
}
函数日志错误(发送方,参数){
console.log('发生错误:'+args.get_message());
}
函数日志成功(发送方,参数){
log('Copied items');
}
功能复制项目(项目){
$.when.apply(items.get_data().forEach(函数(sourceItem){cloneItem(sourceItem);}))
。然后(logSuccess,logError);
}
功能克隆项(sourceItem){
var deferred=$.deferred();
var ctx=sourceItem.get_context();
var itemCreateInfo=new SP.ListItemCreationInformation();
var targetItem=destList.addItem(itemCreateInfo);
var fieldEnumerator=sourceListFieldCollection.getEnumerator();
while(fieldEnumerator.moveNext()){
var of ield=fieldEnumerator.get_current();
//排除某些字段
如果(!oField.get_readOnlyField()&&
oField.get_internalName()!=“附件”&&
!oField.get_hidden()&&
oField.get_internalName()!=“ContentType”)
{
var sourceFieldVal=sourceItem.get_item(oField.get_internalName());
if(sourceFieldVal!=null){
targetItem.set_项(oField.get_internalName(),sourceFieldVal);
}
}
}
targetItem.update();
载重量(目标项);
ctx.executeQueryAsync(
函数(){deferred.resolve();},
函数(发送方,参数){延迟。拒绝(发送方,参数);
});
延迟返回。承诺();
}

希望这能对您有所帮助。

我最终创建了一个应用程序,使用Jquery完成这项工作。它将跨网站或网站集将任何附件从一个列表传输到另一个列表

这里是链接


我必须在两个网站集之间复制列表。我为目标函数getSourceListItems()添加了一个新上下文{var deferred=$.deferred();var ctx=SP.ClientContext.get_current();var ctx1=new SP.ClientContext(“网站集”);this.sourceList=ctx.get_web().get_lists().getByTitle(sourceListTitle);this.sourceListFieldCollection=sourceList.get_fields();this.destList=ctx1.get_web().get_lists().getByTitle(destListTitle);ctx.load(sourceList);ctx.load(sourceListFieldCollection);ctx1.load(destList);}不起作用??我可以复制列表项,但不能复制附件。我还需要复制附件。