如何在Kendo中点击按钮同时删除多个SharePoint列表中的条目?已实现单列表删除,求新增第二列表的方案
要实现点击一次删除按钮同时移除两个SharePoint列表中的对应条目,你需要修改Kendo Grid的destroy传输配置,自定义删除逻辑来发起两次REST请求(因为SharePoint REST API单次请求只能删除一个条目)。以下是具体实现方案:
情况1:两个列表的条目ID完全一致
如果目标条目在两个列表中的Id字段值相同,可以直接并行发起两个删除请求:
destroy: function(options) { const itemId = options.Id; const webUrl = _spPageContextInfo.webAbsoluteUrl; const requestDigest = $('#__REQUESTDIGEST').val(); // 封装删除单个列表条目的通用函数 const deleteListItem = (listTitle, targetId) => { return $.ajax({ url: `${webUrl}/_api/Web/Lists/GetByTitle('${listTitle}')/items(${targetId})`, type: 'DELETE', headers: { "X-RequestDigest": requestDigest, 'Accept': 'application/json; odata=verbose', "If-Match": "*" } }); }; // 并行删除两个列表中的条目 Promise.all([ deleteListItem('GalleryItems', itemId), deleteListItem('你的第二个列表名称', itemId) // 替换为实际列表名 ]) .then(() => { // 两个删除操作均成功,通知Kendo Grid更新UI options.success(); }) .catch(error => { // 处理删除失败的情况 console.error('删除操作失败:', error); options.error(error); }); }, // 列中的Destroy命令配置保持不变 command: ["edit", { name: "destroy", text: "Delete", class: "btn-delete" }], title: " "
情况2:两个列表通过自定义字段关联(条目ID不同)
如果第二个列表的条目是通过其他唯一字段(比如GalleryItemUniqueId或自定义关联字段)与第一个列表关联的,需要先查询到第二个列表中对应的条目ID,再执行删除:
destroy: function(options) { const itemId = options.Id; const webUrl = _spPageContextInfo.webAbsoluteUrl; const requestDigest = $('#__REQUESTDIGEST').val(); // 封装删除单个列表条目的通用函数 const deleteListItem = (listTitle, targetId) => { return $.ajax({ url: `${webUrl}/_api/Web/Lists/GetByTitle('${listTitle}')/items(${targetId})`, type: 'DELETE', headers: { "X-RequestDigest": requestDigest, 'Accept': 'application/json; odata=verbose', "If-Match": "*" } }); }; // 查询第二个列表中关联的条目ID const getRelatedItemId = () => { return $.ajax({ url: `${webUrl}/_api/Web/Lists/GetByTitle('你的第二个列表名称')/items?$select=Id&$filter=关联字段名称 eq ${itemId}`, // 替换关联字段名 type: 'GET', headers: { 'Accept': 'application/json; odata=verbose' } }).then(response => { const relatedItems = response.d.results; if (relatedItems.length > 0) { return relatedItems[0].Id; } throw new Error('未找到关联的条目'); }); }; // 先查询关联ID,再执行双列表删除 getRelatedItemId() .then(relatedItemId => { return Promise.all([ deleteListItem('GalleryItems', itemId), deleteListItem('你的第二个列表名称', relatedItemId) ]); }) .then(() => { options.success(); }) .catch(error => { console.error('删除操作失败:', error); options.error(error); }); }, // 列中的Destroy命令配置保持不变 command: ["edit", { name: "destroy", text: "Delete", class: "btn-delete" }], title: " "
关键说明
- 替换代码中的
你的第二个列表名称和关联字段名称为实际的SharePoint列表名称和关联字段; - 使用
Promise.all确保两个删除操作都完成后,才通知Kendo Grid更新界面,避免UI不同步; - 错误处理部分可以根据需求扩展,比如添加用户友好的错误提示;
- 保持
If-Match: "*"头,确保可以删除已修改过的条目(如果不需要版本控制,可以保留)。
内容的提问来源于stack exchange,提问作者Daniel
相关产品推荐
相关产品推荐

