locked
how to get id of newest item in the document library RRS feed

  • Question

  • i hvae applied the below code and i got the id of all the item present in the document library.i want to retrive the newest's item id.here is my code.:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/2014.02/jquery.SPServices-2014.02.min.js"></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('docLib1');
            var query = SP.CamlQuery.createAllItemsQuery();
            allItems = list.getItems(query);
            context.load(allItems);
            context.executeQueryAsync(Function.createDelegate(this, this.successGetListItemId));
        }

        function successGetListItemId() {
                    
            var TextFiled = "";
            var ListEnumerator = this.allItems.getEnumerator();
            while (ListEnumerator.moveNext()) {
                var currentItem = ListEnumerator.get_current();
                TextFiled += currentItem.get_item('ID');
            }
            alert(TextFiled);
        }
    </script>
    </script>
    <input type="button" onclick="GetListItemId()" value="Copy">

    Wednesday, December 7, 2016 11:28 AM

Answers

  • Hey,

    Can you try to update your caml query to sort the result by ID in desc order?

    Have below function for your reference.

    function GetListItemId() {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('docLib1');
        var query = new SP.CamlQuery();
        query.set_viewXml('<View><Query><OrderBy><FieldRef Name="ID" Ascending="False"></FieldRef></OrderBy></Query><RowLimit>1</RowLimit></View>');
        allItems = list.getItems(query);
        context.load(allItems);
        context.executeQueryAsync(Function.createDelegate(this, this.successGetListItemId));
    }

    Thanks.
    Please mark it as an answer if it helps.

    • Marked as answer by laxmikant.b2 Thursday, December 8, 2016 8:26 AM
    Wednesday, December 7, 2016 2:34 PM

All replies

  • Hi

    another easier approach

    Create a secodnary list,

    Attach a workflow to your library , which will start when a doc is added to your list , and which will update a field in that secondary list  with Creation date

    Use that informations when necessary


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, December 7, 2016 11:50 AM
  • Hey,

    Can you try to update your caml query to sort the result by ID in desc order?

    Have below function for your reference.

    function GetListItemId() {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        var list = web.get_lists().getByTitle('docLib1');
        var query = new SP.CamlQuery();
        query.set_viewXml('<View><Query><OrderBy><FieldRef Name="ID" Ascending="False"></FieldRef></OrderBy></Query><RowLimit>1</RowLimit></View>');
        allItems = list.getItems(query);
        context.load(allItems);
        context.executeQueryAsync(Function.createDelegate(this, this.successGetListItemId));
    }

    Thanks.
    Please mark it as an answer if it helps.

    • Marked as answer by laxmikant.b2 Thursday, December 8, 2016 8:26 AM
    Wednesday, December 7, 2016 2:34 PM