【问题标题】:Copy SharePoint list items from one list to another with attachments using j query, JavaScript or rest API?使用 j 查询、JavaScript 或 REST API 将 SharePoint 列表项从一个列表复制到另一个列表?
【发布时间】:2017-12-14 08:25:08
【问题描述】:

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

我使用 Excel 导出和导入,但它没有带回附件。

非常感谢任何帮助。

【问题讨论】:

    标签: javascript jquery sharepoint-2013 office365api sharepoint-list


    【解决方案1】:

    您可以使用以下 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>
    

    希望这会对你有所帮助。

    【讨论】:

    • 我必须在两个网站集之间复制列表。我为目标函数 getSourceListItems() { var deferred = $.Deferred(); 添加了一个新上下文var ctx = SP.ClientContext.get_current(); var ctx1 = new SP.ClientContext("new 网站集"); 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);} 不起作用??
    • 我能够复制列表项,但不能复制附件。我也需要复制附件。
    【解决方案2】:

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

    这里是链接。

    https://sshareasolutions.com/2019/01/10/transfer-list-attachments-across-site-and-sitecollection/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 2015-06-07
      • 2013-04-27
      • 2014-10-05
      • 1970-01-01
      相关资源
      最近更新 更多