且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何限制可在 SharePoint 列表中输入的项目数?

更新时间:2023-01-24 10:00:04

没有任何内置功能可以让您完成此操作.但是,如果您提供自己的用于创建条目的界面(例如自定义注册页面),您可以控制行为并在达到限制后阻止注册.

There's nothing built-in to allow you to accomplish this. However, if you present your own interface for creating entries (such as a custom sign-up page) you can control the behavior and prevent signups after the limit is reached.

为了帮助您入门,这里有一个使用 JavaScript 对象模型来查询列表并在创建项目之前检测项目计数的示例.

To get you started, here's an example of using the JavaScript object model to query the list and detect the item count before creating an item.

<input type="button" value="Sign Up Now!" onclick="createItemIfBelowLimit()" />
<script>
function createItemIfBelowLimit(){
    var max = 60;
    var listTitle = "Your List Title";
    var clientContext = new SP.ClientContext();
    var list = clientContext.get_web().get_lists().getByTitle(listTitle);
    clientContext.load(list);
    clientContext.executeQueryAsync(function(){
        var itemCount = list.get_itemCount();
        if(itemCount < max){
            createItem(listTitle,{
                "Title":"Example title text",
                "Body":"Example body text"
                });         
        }else{
            alert("This sign-up list is full. Sorry!");
        }
    },function(sender,args){
        alert(args.get_message());
    });
}
function createItem(listTitle,values){
    var clientContext = new SP.ClientContext();
    var list = clientContext.get_web().get_lists().getByTitle(listTitle);
    var newItem = list.addItem();
    for(var key in values){
        newItem.set_item(key,values[key]);
    }
    newItem.update();
    clientContext.load(newItem);
    var rootFolder = list.get_rootFolder(); // Note: use a list's root folder to determine its server relative URL
    clientContext.load(rootFolder);
    clientContext.executeQueryAsync(function(){
        var itemId = newItem.get_item("ID");
        SP.UI.ModalDialog.showModalDialog(
            { 
                title: "Item #"+itemId+" Created Successfully!", 
                url: rootFolder.get_serverRelativeUrl() + "/DispForm.aspx?ID="+itemId
            }
        ); 
    },function(sender,args){
        alert(args.get_message());
    });
}
</script>

要使用上述代码,请先将其保存为文本文件,然后将其上传到 SharePoint 网站上的库中.然后,您可以在您的网站上创建一个页面,将内容编辑器 Web 部件添加到该页面,并编辑该内容编辑器 Web 部件的 Web 部件属性.在内容链接"属性中,粘贴文本文件的链接;这将导致 Web 部件呈现您的 HTML 和 JavaScript.

To use the above code, first save it to a text file and upload it to a library on your SharePoint site. Then you can then create a page on your site, add a content editor web part to the page, and edit the web part properties of that content editor web part. In the "Content Link" property, paste in the link to the text file; this will cause the web part to render your HTML and JavaScript.

如需进一步阅读,请查看 Common微软提供的 JavaScript 对象模型文档中的编程任务.

For further reading, check out the Common Programming Tasks in the JavaScript Object Model documentation provided by Microsoft.