0
votes

Trying to fetch the last modified time of Sharepoint list. Found a code that states the author and modified by. It was working fine when it had the line

<input id="btnGetFieldUserValue" onclick="getFieldUserValue()" type="button" value="Get Created by and Modified by"/>

but when i changed to

<div id="create" onload="getFieldUserValue()"> &#160;</div>

No repose in the content editor webpart of sharepoint 2013.

Help.

My task is to show the last updation time of the list. Any other way to do that, please share.

    var listItem;
    var list;
    var clientContext;

    function getFieldUserValue() {

        this.clientContext = SP.ClientContext.get_current();
        if (this.clientContext != undefined && clientContext != null) {
            var webSite = clientContext.get_web();
            this.list = webSite.get_lists().getByTitle("Resource Readiness");
            this.listItem = list.getItemById(1);
            clientContext.load(this.listItem);
            this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
        }
    }

    function OnLoadSuccess(sender, args) {
        var fieldUserValueCreatedBy = this.listItem.get_item("Author");
        var fieldUserValueModifiedBy = this.listItem.get_item("Editor");
        document.getElementById("create").innerHTML = fieldUserValueModifiedBy.get_lookupValue() ;
        alert("Created By: " + fieldUserValueModifiedBy.get_lookupValue() + "\n Modified By: " + fieldUserValueModifiedBy.get_lookupValue() + "\n");
    }


    function OnLoadFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }</script> 
 
1

1 Answers

0
votes

You have to wait for script files:

     function getFieldUserValue(){
           SP.SOD.executeOrDelayUntilScriptLoaded(loadContext, 'sp.js');
     }
     function loadContext() {
            this.clientContext = SP.ClientContext.get_current();
        if (this.clientContext != undefined && clientContext != null) {
            var webSite = clientContext.get_web();
            this.list = webSite.get_lists().getByTitle("Resource Readiness");
            this.listItem = list.getItemById(1);
            clientContext.load(this.listItem);
            this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
        }
     }

    function OnLoadSuccess(sender, args) {
        var fieldUserValueCreatedBy = this.listItem.get_item("Author");
        var fieldUserValueModifiedBy = this.listItem.get_item("Editor");
        document.getElementById("create").innerHTML = fieldUserValueModifiedBy.get_lookupValue() ;
        alert("Created By: " + fieldUserValueModifiedBy.get_lookupValue() + "\n Modified By: " + fieldUserValueModifiedBy.get_lookupValue() + "\n");
    }


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