批量更新Sharepoint 2013列表中的项目

时间:2014-09-26 19:51:08

标签: javascript sharepoint office365

我有一个列表,需要为其所有项目更改日期。由于我们使用的是O365,并且我希望用户自己更新列表,因此我必须使用HTML / JavaScript。

到目前为止,我能够获取列表 - 通过它进行枚举 - 甚至可以将值更新到网页。

我很想知道如何更新列表中的所有行。我有一个C#项目,它可以实现这一功能,但JavaScript却截然不同。

以下是检索和更新日期的核心代码。我尝试创建一个像c#代码那样更新每一行的函数 - 但是我从firebug中获取了一条树被破坏的消息。 我试图通过谷歌解决方案来解决问题。我似乎找到的只是如何更新一行。

$(function () {
    retrieveAllListProperties();
    $('#siteLists').change(testMessage);
    $('#datepicker').change(testMessage);
    $("#datepicker").datepicker();
});



function testMessage() {
    var selectedValue = $("#siteLists option:selected").val();
    if (selectedValue != null) {
        retrieveListItems();
    }
}

function retrieveListItems() {
    var clientContext = new SP.ClientContext();
    var selecteList = $("#siteLists option:selected").val();
    var oList = clientContext.get_web().get_lists().getByTitle(selecteList);

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' +
        '<RowLimit></RowLimit></View>');

    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
    );
}
function onQuerySucceeded(sender, args) {
    var listItemInfo = '<tr>' +
            ' <th ><strong>ID: </strong> ' + '</td>' +
            ' <th style="width:40%"><strong>Title</strong> ' + '</td>' +
            ' <th ><strong>WD</strong> ' + '</td>' +
            ' <th ><strong>Due Date:</strong> ' + '</td>' +
            ' </tr>';
    var listItemEnumerator = collListItem.getEnumerator();
    var select = document.getElementById("selectNumber");
    var setStop = true;
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var listItemId = oListItem.get_id();

        var newDate = $("#datepicker").val();
        if (newDate != "") {
            var wd = oListItem.get_item('WD')
            var datedue = businessDays(wd, newDate);
            oListItem.set_item('TimeCST', datedue);
            oListItem.set_item('DueDate', datedue);
            oListItem.update();
        } else {
            datedue = oListItem.get_item('DueDate')

        }
        listItemInfo += '<tr>' +
            ' <td>' + oListItem.get_id() + '</td>' +
            ' <td>' + oListItem.get_item('Title') + '</td>' +
            ' <td>' + oListItem.get_item('WD') + '</td>' +
            ' <td>' + datedue + '</td>' +
            ' </tr>';

    }
    $("#ListData").html(listItemInfo);
}
function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}

function updatelistvalues() {
    //recurse through the list and build update query based on wd and date value in text
    var clientContext = new SP.ClientContext();
    var selecteList = $("#siteLists option:selected").val();
    var oList = clientContext.get_web().get_lists().getByTitle(selecteList);
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' +
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' +
        '<RowLimit></RowLimit></View>');
    var collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
       Function.createDelegate(this, this.onQueryupdatelistvaluesSucceeded),
       Function.createDelegate(this, this.onQueryupdatelistvaluesFailed)
    );
}

function onQueryupdatelistvaluesSucceeded(sender, args) {
    var newDate = $("#datepicker").val();
    var wd = -5
    var datedue = businessDays(wd, newDate);
    alert(datedue);
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();
    var select = document.getElementById("selectNumber");
    alert('onQuerySucceeded ' + listItemEnumerator.get_id);

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        //calculate the new due date
        var wd = oListItem.get_item('WD');
        var newDate = $("#datepicker").val();
        var datedue = businessDays(wd, newDate);              //string for 3 days before now (today)
        alert(datedue)
    }
    alert("listItemInfo: ");
    $("#ListData").html(listItemInfo);
}

function updateItem(ListName, datedue, listItemId) {
    var clientContext = new SP.ClientContext();
    var selecteList = $("#siteLists option:selected").val();
    var oList = clientContext.get_web().get_lists().getByTitle(selecteList);

    listItemToUpdate = oList.getItemById(listItemId);
    listItemToUpdate.set_item('DueDate', datedue);
    listItemToUpdate.update();

    clientContext.executeQueryAsync(
        Function.createDelegate(this, success),
        Function.createDelegate(this, fail)
        );

}
function success(sender, args) {
    alert('success');
}
function onQueryupdatelistvaluesFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}

/* get all lists in site*/
function retrieveAllListProperties(siteUrl) {
    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web();
    this.collList = oWebsite.get_lists();
    clientContext.load(collList);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onListQuerySucceeded),
        Function.createDelegate(this, this.onListQueryFailed)
    );
}

function onListQuerySucceeded() {

    var select = document.getElementById("siteLists");
    var listInfo = '';
    var listEnumerator = collList.getEnumerator();

    while (listEnumerator.moveNext()) {
        var oList = listEnumerator.get_current();
        var listTemplateNum = oList.get_baseTemplate();
        if (listTemplateNum == 171) {
            var el = document.createElement("option");
            el.textContent = oList.get_title();
            el.value = oList.get_title();
            select.appendChild(el);
        }
    }
}

function onListQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() +
        '\n' + args.get_stackTrace());
}

1 个答案:

答案 0 :(得分:0)

如果切换到“executeQuery”而不是“executeQueryAsync”会发生什么?我想现在太多的异步操作会运行,导致奇怪的问题。