如何防止Sharepoint添加新列表项限制超过5个,而不是查看项目

如何防止Sharepoint添加新列表项限制超过5个,而不是查看项目,sharepoint,Sharepoint,共享点添加列表项计数大于5显示验证,是否可以使用验证。如果没有,请告诉我流程。我们可以使用REST API获取当前用户的项目计数,然后在新表单页面中阻止新项目 例如,将下面的代码添加到newform.aspx页面的脚本编辑器web部件中 <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script> <script type="text/javas

共享点添加列表项计数大于5显示验证,是否可以使用验证。如果没有,请告诉我流程。

我们可以使用REST API获取当前用户的项目计数,然后在新表单页面中阻止新项目

例如,将下面的代码添加到newform.aspx页面的脚本编辑器web部件中

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
var listTitle="CL0902";
$(function () {         
    var itemCount=getItemsCountByAuthor(listTitle);
    if(itemCount >= 5){
        alert("You can not add item more than 5.");
        window.location.href=_spPageContextInfo.webAbsoluteUrl+"/lists/"+listTitle+"/AllItems.aspx";
    }
});
function PreSaveItem(){
    var itemCount=getItemsCountByAuthor(listTitle);
    if(itemCount >= 5){
        alert("You can not add item more than 5.");
        return false;
    }
    return true;
}
function getItemsCountByAuthor(listTitle){
    var itemCount=0;
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('"+listTitle+"')/items?$select=Author/ID,ID&$expand=Author&$filter=Author/Id eq "+_spPageContextInfo.userId,
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            itemCount=data.d.results.length;        
        },
        error: function (data) {
            //alert("Error");
        }
    });
    return itemCount;
}
</script>

var listTitle=“CL0902”;
$(函数(){
var itemCount=getItemScontByAuthor(listTitle);
如果(itemCount>=5){
警告(“您不能添加超过5项的项”);
window.location.href=\u spPageContextInfo.webAbsoluteUrl+“/lists/”+listTitle+“/AllItems.aspx”;
}
});
函数PreSaveItem(){
var itemCount=getItemScontByAuthor(listTitle);
如果(itemCount>=5){
警告(“您不能添加超过5项的项”);
返回false;
}
返回true;
}
函数GetItemScontByAuthor(listTitle){
var itemCount=0;
$.ajax({
url:_spPageContextInfo.webAbsoluteUrl+“/_api/web/lists/getbytitle(“+listTitle+”)/items?$select=Author/ID,ID&$expand=Author&$filter=Author/ID eq”+_spPageContextInfo.userId,
键入:“获取”,
async:false,
标题:{
“接受”:“application/json;odata=verbose”,
},
成功:功能(数据){
itemCount=data.d.results.length;
},
错误:函数(数据){
//警报(“错误”);
}
});
返回项目计数;
}

我可以将上述代码写入direct list/mylist/newform.aspx吗?无法在web部件中插入新表单。我不知道发生了什么你用现代的列表形式吗?在经典列表表单中,我们可以编辑页面并将脚本编辑器web部件添加到页面中。实际上,我从mylist中的Newform.aspx中获取了代码,然后在页面中创建一个自定义页面,然后将该代码添加到带有名称空间的新页面中。我尝试使用json公式和ColumValidation进行验证,但没有达到使用类似的东西进行列验证的预期。但是这是基于日期的。如果您使用自定义的新表单页面,我们需要监视“Save”按钮单击事件,然后调用getItemsCountByAuthor方法进行验证。