Domanda

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>
È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top