Show Elapsed Modified Time in a Document Library

Written by:

Introduction

The requirement has come to fruition to see the elapsed time from the “Modified Date” to “Now” on a reports document library. This is not possible in a calculated value unfortunately do to [Today] and [Me] values being consumed in a calculated value are considered “Volatile” by SharePoint out of the box. These use the core MOSS/WSS functions and since the list is 100% dynamic it doesn’t pull session or current state information before loading the list on page load. Every possible solution was attempted prior to using this custom solution. Although this is all “client side” customization all methods of using out of the box methods were tried prior to this solutions development, the downfall being that even when [Today] was able to be used in a calculated value it did not record the time, only the current date.

Required Items

This solution involves slight modification of Meta Data through a simple SharePoint Designer workflow, a short JavaScript function, and the creation of a “Last Updated” field that is a Date and Time field. Below are step by step instructions with screen shots of how to properly customize any List or Document Library to use this functionality. SharePoint Designer, .NET Frame Work 3.5, and WSS 3.0 are required to implement this solution.

Elapsed Time

Create or open an existing document library and go to settings.

Figure 1 Document Library Settings

Click Create Column

Figure 2 Create Column

Name the Column “Last Updated” (Red) and make it a Date & Time column (Blue)

Figure 3 Configure Column


Click Save, and go back to the Document Library page. Click Site Actions, and Edit Page.

Figure 4 Edit Page

Click Add a Web Part and select a Content Editor Web Part and click add.

Figure 5 Add a Web Part

Figure 6 Content Editor Web Part

Select Edit on the Content Editor Web Part and select Modified Shared Web Part

Figure 7 Modify Web Part

Select source editor and copy the following into the source box and click save.

I used the below code from this website: http://www.endusersharepoint.com/2009/01/23/jquery-for-everyone-replacing-today/

Source:

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js” type=”text/javascript”></script>

<script type=”text/javascript”>

$(function() {

var str = “Last Updated”; //change this based on col header

var today = new Date();

today = Date.parse(today)/1000;

var a=0;

var headers = $(“table.ms-listviewtable:first> tbody> tr:first th”).get();

$.each(headers, function(i,e){

x = $(e).contents().find(“a[title*='"+str+"']“).length;

a = x > 0 && i > a ? i : a;

});

var dArray = $(“table.ms-listviewtable:first> tbody> tr:gt(0)”).find(“>td:eq(“+a+”)”).get()

$.each(dArray, function(i,e){       �

var d1 = Date.parse($(e).text())/1000;

var dd = (today-d1)/86400;

var dh = (dd-Math.floor(dd))*24;

var dm = (dh-Math.floor(dh))*60;

var time = ((Math.floor(dd) > 0 ? Math.floor(dd) +” days, ” : “”)+

(Math.floor(dh) > 0 ? Math.floor(dh)+” hrs, ” : “”)+

(Math.floor(dm)+” min”));

$(e).text(time);

});

});

</script>

Figure 8 Source Editor Selection

Figure 9 Source Editor Input

Open the page in SharePoint Designer and create a new workflow file.

Figure 10 Create a Workflow

Name the workflow as “SetLUAsModified” select the list to attach as the document library you are using and select on create and on edit as when the workflow will run.

Figure 11 Set Last Updated Time

Click next and now create your Action, we are setting the last updated field to equal modified. Go to the Actions Drop down and select set field in current item (Red), then set value 1 as “Last Updated” and Value 2 as “Current Item” “Modified” (Blue)

Figure 12 Set Field

Figure 13 Set Last Updated

Figure 14 Set Modified

Save the Workflow and go back to the site, add a document to the library or edit an existing document and the “Last Updated” field will change to 0 minutes.

testResult

Figure 15 Test Results

Posted on November 3rd, 2009 in Technology, Tips & Tricks.