locked
Auto-Increment single line of text Field in a SharePoint Online List with threshold limit crossed RRS feed

  • Question

  • Hi

    I am using the below javascript script for auto-incrementing the list items of a SharePoint online list but the items are huge more than 9000+, but the last item is 10307 so next autoincrement number should be 10308, but the script output is 1000 on the new form, please suggest

    I have created index on the column already

    <script src="/sites/test/SiteAssets/jquery-3.5.1.min.js"></script>
    <script src="/sites/test/SiteAssets/sputility.min.js"></script>
    <script>
    
        // Get the current Site
        var siteUrl = 'https://abc.sharepoint.com/sites/test/';
    
        function retrieveListItems() {
    
            var clientContext = new SP.ClientContext(siteUrl);
    
            // Get the liste instance
            var oList = clientContext.get_web().get_lists().getByTitle('test');
    
            var camlQuery = new SP.CamlQuery();
    
            // Get only the last element
            camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Number\' Ascending=\'False\' /></OrderBy></Query><RowLimit>1</RowLimit></View>');
            this.collListItem = oList.getItems(camlQuery);
    
            clientContext.load(collListItem);
    
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
    
        }
    
        function onQuerySucceeded(sender, args) {
    
            var listItemInfo = '';
    
            var listItemEnumerator = collListItem.getEnumerator();
    
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
    
                var listItemInfovalue = oListItem.get_item('Number');
    
                // increment the index
                var newId = parseInt(listItemInfovalue)+1;
    
                // create the new id
                SPUtility.GetSPFieldByInternalName('Number').SetValue(newId);
    			SPUtility.GetSPFieldByInternalName('Number').MakeReadOnly();
    
            }
    
            console.log(listItemInfo.toString());
        }
    
        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
        }   
    
    $(document).ready(function(){
        ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js"); 
        });
    
    </script>



    Paru

    Monday, July 27, 2020 7:03 AM

All replies

  • Hello Paru,

    I tested in a Large List which have 7980 items also exceeded the List View Threshold in SharePoint Online, it works as expected, the code snippet, just updated the Site Url and List Title:

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script type="text/javascript" src="https://zheguo.sharepoint.com/sites/dev/SiteAssets/sputility.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script type="text/javascript">
    
        // Get the current Site
        var siteUrl = 'https://zheguo.sharepoint.com/sites/dev/';
    
        function retrieveListItems() {
    
            var clientContext = new SP.ClientContext(siteUrl);
    
            // Get the liste instance
            var oList = clientContext.get_web().get_lists().getByTitle('LargeList');
    
            var camlQuery = new SP.CamlQuery();
    
            // Get only the last element
            camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'Number\' Ascending=\'False\' /></OrderBy></Query><RowLimit>1</RowLimit></View>');
            this.collListItem = oList.getItems(camlQuery);
    
            clientContext.load(collListItem);
    
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        
    
        }
    
        function onQuerySucceeded(sender, args) {
    
            var listItemInfo = '';
    
            var listItemEnumerator = collListItem.getEnumerator();
    
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
    
                var listItemInfovalue = oListItem.get_item('Number');
    
                // increment the index
                var newId = parseInt(listItemInfovalue)+1;
    			console.log(newId);
                // create the new id
                SPUtility.GetSPFieldByInternalName('Number').SetValue(newId);
    			SPUtility.GetSPFieldByInternalName('Number').MakeReadOnly();
    
            }
    
            console.log(listItemInfo.toString());
        }
    
        function onQueryFailed(sender, args) {
            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); 
        }   
    
    $(document).ready(function(){
        ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js"); 
        
    });
    </script>
    

    So in the New Form, the Number field should filled with 7981 value automatically as expected:

    In your side, I suggest you debug with Developer Tool to see if oListItem.get_item("Number") has the valid value, should be the largest number field value in the list:

    Thanks

    Best Regards


    "SharePoint" forums will be migrating to a new home on Microsoft Q&A !
    We invite you to post new questions in the "SharePoint" forums' new home on Microsoft Q&A !

    Tuesday, July 28, 2020 6:14 AM