1

For a number of pages within my site, I need to display the date that the page was last modified. I know this is possible using a custom view for the page library, but for my purposes, that is not an acceptable solution.

Using information I found online ( I can't seem to find the source right now), I created a .htm file and linked it to a Content editor web part.

The code within the .htm file is as follows

<HTML xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<HEAD>
<TITLE>Time Stamper</TITLE>

<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:Associated_x0020_with msdt:dt="string"></mso:Associated_x0020_with>
<mso:TemplateUrl msdt:dt="string"></mso:TemplateUrl>
<mso:xd_ProgID msdt:dt="string"></mso:xd_ProgID>
<mso:_CopySource msdt:dt="string">http://theurl/to/the/code/Document/show last mod.htm</mso:_CopySource>
<mso:Order msdt:dt="string">500.000000000000</mso:Order>
<mso:_SourceUrl msdt:dt="string"></mso:_SourceUrl>
<mso:_SharedFileIndex msdt:dt="string"></mso:_SharedFileIndex>
</mso:CustomDocumentProperties>
</xml><![endif]-->
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
update = new Date(document.lastModified)
theMonth = update.getMonth() + 1
theDate = update.getDate()
theYear = update.getFullYear()
document.writeln("<I>Last updated:" + theMonth + "/" + theDate + "/" + theYear + "</I>")
</SCRIPT>
<HR>
</BODY>
</HTML>

This code almost works, but it displays the date the page was last visited, not modified.

2
  • 2
    on your rest query call you want to change the field "timelastmodified" for "Modified" that's the system name for the last time the item or document was modified
    – Mike
    Commented Dec 12, 2017 at 15:59
  • @mike thanks for the tip. I goofed in the original post and pasted the wrong code. now i've put in the correct code that shows the date the page was last visited. I tried replacing 'last modified' in the "Update = " line with 'Modified' but that returned an error. Commented Dec 12, 2017 at 18:57

1 Answer 1

1

We had the same problem here when migrating from 2007. For some reason, it is not included in the new page layouts, so we wrote a script. Hopefully you can get something from this that will help you:

// Fix wiki pages by adding last modified info to the bottom for migrated wikis
function FixMigratedWikiPages() {
    var sItemId = typeof(_spPageContextInfo.pageItemId) == "undefined" ? 0 : _spPageContextInfo.pageItemId;
    var  sListTemplate = typeof(_spPageContextInfo.listBaseTemplate) == "undefined" ? 0 : _spPageContextInfo.listBaseTemplate;

    if (sItemId != 0 && sListTemplate == 119) 
    {
        // Get GUID of the document library where the page is stored
        var spageListId = _spPageContextInfo.pageListId;
        sPageListId = spageListId.replace("{", "'").replace("}", "'");
        // Form the REST URL to get the Last Modified and Last Modified By Information
        var sRestApiRequestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists(guid" + sPageListId + ")/items(" + sItemId + ")?$select=Modified,ID,Editor/Title,Editor/EMail,*&$expand=Editor";

        getWikiData(sRestApiRequestUrl ).then(function (spData) {
            addDateToWikiFooter(spData);
        }); 
    }
}


function addDateToWikiFooter(spData) {
    var strDate = spData.d.Modified;
    var date = new Date(strDate);
    date = formatDate(date);

    var modifiedBy = spData.d.Editor.Title;

    $('*[id*=WikiField]:visible').each(function() {
        var count = $('*[id=wikiModified]:visible').length;
        if(count == 0) {
            $(this).append('<br/><div id="wikiModified" class="ms-descriptiontext">Last modified at ' + date + ' by ' + modifiedBy + '</div>');
            return false;
        }
    }); 
}

function formatDate(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return date.getMonth()+1 + "/" + date.getDate() + "/" + date.getFullYear() + "  " + strTime;
}

function getWikiData(query) {

    var relativePageURL = _spPageContextInfo.serverRequestPath;
    var siteURL = _spPageContextInfo.webAbsoluteUrl;         

    //var query = siteURL + "/_api/web/getfilebyserverrelativeurl('/"+ relativePageURL +"')?$select=TimeLastModified,ModifiedBy/Title,ModifiedBy/LoginName&$expand=ModifiedBy";

    var deferred = $.Deferred();
    $.ajax({
        url: query,
        type: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (results) {
            deferred.resolve(results);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(errorThrown);

        }
    });

    return deferred.promise();
}

$(function() {
    FixMigratedWikiPages();
});

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.