0

I am trying to get the time and date a SharePoint list was last modified. The code to call the date and time will live on a different SharePoint page. I've thrown the code below together based on bits and pieces found online. CurrentTimeFrame is the list name I need to call the information for. This code doesn't work, but I couldn't tell you why. Openly admit I am not much of a JavaScript person. Any pointers (aside from learning the language - which I'm working on)? Help is very much appreciated.

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

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


    $(document).ready(function () {


    var timeFrameRequestUrl = _spPageContextInfo.webAbsoluteUrl + 
"/_api/web/lists/getbytitle('CurrentTimeFrame')/items";


    $.ajax({
        url: timeFrameRequestUrl,
        method: "GET",
        headers: {
            accept: "application/json;odata=verbose"
        }
    }).done(function (response) {
       console.log(response);   



document.write("Last updated on ")
document.write(lastModified() + " @ " + GetTime());
document.write(" [D M Y 24 Hour Clock]")
document.write("");

        });

    })
})
</script>
2
  • You want to get the date the List was modified, or you want to get the date the most recently modified item in the List was modified?
    – willman
    Commented Jan 22, 2020 at 18:34
  • The date of the most recent modification to any of the items in the list. Basically if the list was modified in any way, the date of that modification.
    – mehumphry
    Commented Jan 22, 2020 at 18:42

1 Answer 1

1

If you execute the items API call without any filter, you will get a result set that contains all the items in that list. Maybe not a problem if the list only contains a small number of items, but obviously lists can potentially contain thousands of items and that would not perform well. The following example gets just retrieves the most recently modified item in the list and logs the raw result set and the Item's Modified date to the console:

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


    $(document).ready(function () {


    var timeFrameRequestUrl = _spPageContextInfo.webAbsoluteUrl +  "/_api/web/lists/getbytitle('CurrentTimeFrame')/items?$top=10&$orderby=Modified desc";


    $.ajax({
        url: timeFrameRequestUrl,
        method: "GET",
        headers: {
            accept: "application/json;odata=verbose"
        },
        success: function (results){
            console.log(results)
            console.log(results.d.results[0].Modified)
        },
        error: function (error) {
            console.log("error occurred")
            console.log(error);
        }
    })   

        });

    })
})
</script>

For more details on the SharePoint REST API, see here.

1
  • I can't thank you enough for your help!
    – mehumphry
    Commented Jan 23, 2020 at 14:00

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.