How to Limit the number of items that can be entered in a SharePoint List?

懵懂的女人 提交于 2021-02-08 11:50:50

问题


I am trying to limit the number of entries allowed on a SharePoint 2010 list . The list is for users to sign up to and we want to limit this to 60 for example. I have had a look at the similar questions but none of them seem to work.

None of the validation formulas found so far have been appropriate. I would appreciate any advice and help on this .

Thank you is advance ! :)


回答1:


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.

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>

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.

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



来源:https://stackoverflow.com/questions/36796007/how-to-limit-the-number-of-items-that-can-be-entered-in-a-sharepoint-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!