将属性推送到数组,从数组中提取属性

时间:2018-03-14 13:41:16

标签: javascript arrays sharepoint

我正在尝试将SharePoint列表项中的多个属性打包到一个数组中(仅在视图中选择项目),将数组传递给函数并解压缩多个属性以在另一个列表中创建列表项,简而言之,我正在尝试将列表项中的某些字段从一个列表复制到另一个列表。

问题是我不确定如何从getselected函数中打包属性并在writeSelected函数中解压缩它们。我花了很多时间在互联网和堆栈网站上搜索有关如何做到这一点但没有成功的信息。充其量我的结果未定义......

main.cpp: In instantiation of 'bool compare::less(const T&, const T&) [with T = my_type; auto P = &my_type::i; auto ...Ps = {&my_type::s}]':
main.cpp:42:88:   required from here
main.cpp:16:23: error: no matching function for call to 'less<&my_type::s>(const my_type&, const my_type&)'
     return less<Ps...>(t1, t2);
            ~~~~~~~~~~~^~~~~~~~
main.cpp:6:7: note: candidate: template<class T, auto P, auto ...Ps> bool compare::less(const T&, const T&)
  bool less(const T &t1, const T &t2)
       ^~~~
main.cpp:6:7: note:   template argument deduction/substitution failed:

1 个答案:

答案 0 :(得分:0)

如果要将列表项从一个列表复制到另一个列表,请参考以下代码:

<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>

<强>参考

How to copy SharePoint list items from one list to another

Copy List items using REST/JSON