You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改SharePoint列表提交脚本,实现单用户仅执行一次提交以避免重复记录

解决SharePoint列表重复添加记录的问题

首先咱们得揪出问题的根源:你的代码在页面加载完成后($(document).ready 里)就自动调用了 AddListItem 函数,这就是刷新页面会重复加记录的核心原因——每次刷新页面都会触发一遍这个自动执行的逻辑,再加上点击按钮也会触发,等于双重触发了添加操作。

下面给你两种解决方案,从快速实现到更严谨的版本都有:

方案一:客户端标记(快速实现,基于浏览器存储)

这个方案用浏览器的 localStorage 来记录用户是否已经提交过,同一用户在同一浏览器里只会提交一次,实现起来很简单。

修改步骤:

  1. 删掉页面自动执行的代码:把 $(document).ready 里的 ExecuteOrDelayUntilScriptLoaded(AddListItem, "sp.js"); 移除,让添加操作只在用户点击按钮时触发。
  2. 在 AddListItem 里加验证逻辑:函数开头先检查是否已有提交记录,成功提交后设置标记。

修改后的完整代码:

<script type="text/javascript"> 
function AddListItem(){ 
    // 检查用户是否已经提交过记录
    if(localStorage.getItem('hasSubmittedMyList')){
        alert('你已经提交过记录啦,不用重复操作哦!');
        return;
    }

    var ListName = "MyList"; 
    var context = new SP.ClientContext.get_current(); 
    var lstObject = context.get_web().get_lists().getByTitle(ListName); 
    var listItemCreationInfo = new SP.ListItemCreationInformation(); 
    var newItem = lstObject.addItem(listItemCreationInfo); 
    newItem.set_item('Title', 'This is new item'); 
    newItem.update(); 
    context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure)); 
    
    function onSuccess() { 
        alert('Item created: ' + newItem.get_id());
        // 提交成功后,给当前用户标记已提交
        localStorage.setItem('hasSubmittedMyList', 'true');
    } 
    function onFailure(sender, args) { 
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
    } 
} 
</script> 
<html> 
<body> 
<input type="button" value="submit" id="btnSub" onclick="AddListItem();" /> 
</body>
</html>

方案二:服务器端验证(更严谨,基于SharePoint列表查询)

如果担心用户清除浏览器缓存导致重复提交,或者需要严格确保每个用户仅提交一次,咱们可以在添加前先查询当前用户是否已经在 MyList 里有记录,只有没有记录时才执行添加操作。

修改后的 AddListItem 函数:

function AddListItem(){ 
    var ListName = "MyList"; 
    var context = new SP.ClientContext.get_current(); 
    var web = context.get_web();
    var currentUser = web.get_currentUser();
    var lstObject = web.get_lists().getByTitle(ListName);

    // 构造CAML查询:查找当前用户创建的记录(可根据你的需求调整查询条件)
    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Eq><FieldRef Name="Author" LookupId="True"/>' +
        '<Value Type="Integer">' + currentUser.get_id() + '</Value></Eq></Where></Query></View>'
    );
    var existingItems = lstObject.getItems(camlQuery);
    context.load(currentUser);
    context.load(existingItems);

    context.executeQueryAsync(
        function(){
            // 检查是否已有记录
            if(existingItems.get_count() > 0){
                alert('你已经提交过记录啦!');
                return;
            }
            // 没有记录则执行添加逻辑
            var listItemCreationInfo = new SP.ListItemCreationInformation(); 
            var newItem = lstObject.addItem(listItemCreationInfo); 
            newItem.set_item('Title', 'This is new item'); 
            newItem.update(); 
            context.executeQueryAsync(
                function(){
                    alert('Item created: ' + newItem.get_id());
                },
                function(sender, args){
                    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
                }
            );
        },
        function(sender, args){
            alert('查询记录失败: ' + args.get_message() + '\n' + args.get_stackTrace());
        }
    );
}

同样记得移除页面加载时自动调用 AddListItem 的代码,让操作只通过按钮触发。

额外优化:防止重复点击

不管用哪种方案,都可以在点击按钮后禁用按钮,避免用户短时间内重复点击:

<input type="button" value="submit" id="btnSub" onclick="AddListItem(); this.disabled = true;" />

或者在 AddListItem 函数里控制按钮状态,失败时重新启用:

function AddListItem(){ 
    var btn = document.getElementById('btnSub');
    btn.disabled = true;

    // 后续逻辑...

    // 在失败回调里重新启用按钮
    function onFailure(sender, args) { 
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
        btn.disabled = false;
    } 
}

内容的提问来源于stack exchange,提问作者user95039

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 17:32:33