I need a JavaScript to add sequence number based on sequence number in last added item (I do not want to use ID for increment in sequence).

Eg: Auto sequence column is with value 1 ... on adding second item value on last item should be checked and sequence as 2 in new item . Since , Sequence column will get reset by workflow as 1 based on some condition i do not want to use ID as increment. I need JavaScript to add increatment value of 1 for new items.

Please help.

2 Answers 2


You could use this rest api to get the last added item sequence column when you create an item.

/_api/web/lists/getbytitle('listTitle')/items?$select=sequenceColumn&$orderby=Created desc

The first value in the return values is the value you want.


Since you've mentioned that the [Sequence Number] can be reset by Workflow, I'm providing another option to retrieve the max value of a [Number] column at any given time, regardless when item was created. This is based on JavaScript and CAML Query.

Here is the script with HTML. To test it, add the script to a [Script Editor] Web-Part on the View Page


<input type='button' value='Get Max Number' onclick="javascript:GetMaxNumber();"/>


<script language="javascript" type="text/javascript">

var listItems;
var listName="Test1";

function GetMaxNumber()
    var ctx = new SP.ClientContext.get_current();
    var olist = ctx.get_web().get_lists().getByTitle(listName);

    var caml = new SP.CamlQuery();
    //Build CAML Query with View Options
     caml.set_viewXml("<View>" + "<Query><GroupBy Collapse='TRUE' GroupLimit='1'><FieldRef Name='MyNumber' Ascending='FALSE'/></GroupBy></Query>" + "<ViewFields><FieldRef Name='ID'/><FieldRef Name='MyNumber'/></ViewFields><RowLimit>1</RowLimit>" + "<Aggregations Value='On'><FieldRef Name='MyNumber' Type='MAX'/></Aggregations>" + "</View>");

     //alert("caml view: " + caml.get_viewXml());

        this.listItems = olist.getItems(caml);                   

        ctx.executeQueryAsync(Function.createDelegate(this, this.onSucceededCallback),Function.createDelegate(this, this.onFailedCallback));


function onSucceededCallback(sender, args)
    //if(this.listItems==null)    { alert('The returned object is null'); }
    //else  {
    //   alert('In success delegate, object retrieved'); } 

    var enumerator = this.listItems.getEnumerator();

        var li = enumerator.get_current();
        var Id= li.get_item('ID');
    var MaxSeqNumber= li.get_item('MyNumber');

        alert("ID: " + Id + ", MaxSequenceNumber: " + MaxSeqNumber);


function onFailedCallback(sender, args)
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());  


enter image description here

Your Answer

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

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