1

Hello and thanks in advance for the help.

I Have a list with 2 columns, Title and IntakeID. I'm attempting to get the next ID in the SharePoint list and input it to the IntakeID field in the newform.aspx.

I have been successful with my script with the exception that each time I add a new item, each item ID is appended. For example: Last Item ID added in list = 6

Add New item the 1st time: value =7

Add New item 2nd time: value = 78

Add New item 3rd time: value = 789

Add New item 4th time: value = 78910

Any help would be greatly appreciated. Thanks!

All I want to return is just the next ID which is 7 in this example.

<script src="http://MYSharePoint.com/sites/TestSite/SiteAssets/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function () {               
        ExecuteOrDelayUntilScriptLoaded(GetListItemId, "sp.js");          
    });

    function GetListItemId() {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('Separation Intake Form');
        var query = SP.CamlQuery.createAllItemsQuery();
        allItems = list.getItems(query);
        context.load(allItems);
        context.executeQueryAsync(Function.createDelegate(this, this.successGetListItemId));
     }

     function successGetListItemId() {
         var TextField = "";
         var ListEnumerator = this.allItems.getEnumerator();
         while (ListEnumerator.moveNext()) {
             var currentItem = ListEnumerator.get_current();
             TextField += currentItem.get_item('ID');
         }
         alert("Adam Testing next ID: " + TextField);
         $("input[title='IntakeID']").val(TextField);
         $("input[title='IntakeID']").attr("readonly","true").css('background-color','#F6F6F6');
     }
</script>
3
  • what SharePoint version are you using? 2010? Oct 9, 2017 at 18:46
  • Also, the question is really confusing. You need to get the "next ID in the SharePoint list". What does next ID mean? You mean the latest List Item ID that is added + 1? or something else? You said that all you want is to return last ID which is 7. But it does not look like it's the last. You could see yourself, that your code also retrieved items with Id = 8, 9 and 10 Oct 9, 2017 at 18:50
  • correct. The latest List Item ID + 1
    – Adam
    Oct 10, 2017 at 13:35

1 Answer 1

3

In your loop under successGetListItemId(), you're looping through all items in the list and appending ID to the string, which is why you're ending up with: 78910.

Simplest way to fix this is by changing this line to set rather than append:

TextField = currentItem.get_item('ID');

Assuming they are ordered right, you'll end up with the last used id.

That said, the above is not very efficient. If there are 5k items in the list, all 5k are returned. Not to mention that if you hit 5,001+, you'll run into the SP list item limit.

To be more efficient, adjust the CAML query to order the list descending based on ID, then return the top 1 item:

var query = new SP.CamlQuery();
query.set_viewXml('<View Scope="Recursive"><Query><OrderBy><FieldRef Name="ID" Ascending="FALSE" /></OrderBy></Query><RowLimit>1</RowLimit></View>');

Code in successGetListItemId() should work just fine at this point.

2
  • Adjusting the CAML query worked like a charm, thank you very much! Well both worked but you made an excellent point with the SP List Item thresholds. Thanks!
    – Adam
    Oct 11, 2017 at 0:20
  • Glad to help! Since it helped resolve the problem you were having, please consider marking this answer as accepted. Thanks so much! Oct 12, 2017 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.