Friday 22 April 2016

Add an "Out of date" alert to SharePoint content pages

I was at an Intranet Experts conference last year in Geneva, courtesy of Advatera. During the day, the subject of how we can get Site Builders in a federated content model to ensure they keep their content up to date. One suggestion was to add a banner to the top of any page that hasn't been updated within a certain time period, for example, a year.

Trouble is, no one really knew how to do it.

So I had a bit of think about it and figured that if I could come up with some way to retrieve the page's Modified value and put that into a JavaScript variable, then it would be easy to get a message to display on the page ... say, like this.



So ... how do you do it? The key is in the SharePoint control:

<SharePointWebControls:FieldValue ID="Modified" FieldName="Modified" runat="server"/>

So, with a lot of help from my colleague David T., this is what we came up with:

<script>
   $( document ).ready(function() {
      var shareDate = '<SharePointWebControls:FieldValue ID="Modified" FieldName="Modified" runat="server"/>';
      var regex = /(\d+)/g;
      var dateParts = shareDate.match(regex);
      var docDate = new Date(dateParts[2]+"/"+dateParts[1]+"/"+dateParts[0]);
      var prevDate = new Date();
      prevDate.setDate(prevDate.getDate() - 365); //This is the number of days in the past to check

      if(docDate < prevDate){
      $( ".Breadcrm" ).append("<strong class='OutOfDate'>The content on this page has not been updated in over a year and may be unreliable</strong>");
}
   });

</script>

To get this work cleanly, I inserted the script at the end of the <div> block that holds the breadcrumb trail in the content page's MasterPage (see the screengrab above for what the final (active) alert looks like.

It's not a foolproof method, because all a content editor has to do is Edit the page then immediately Publish it and the Alert will disappear. But at least it will concentrate the attention of the more diligent content editor.

Hope this helps someone.