0

I have a requirement where in user wants a column to be populated with Current date i.e Today. I went through many posts and found it is not possible to use Today() in list columns.

The reason why user wants this is because he want to use this for calculation purpose. E.g =IF([PCR curative=””; DOY;”PCR curative clousure date”)

What is the efficient method i can advise user. Can anyone guide me on this?

1

A similar question was asked on StackOverflow: Any solution to the Today Calculated Column problem is SharePoint?

There simply isn't a work around for this. As the values for the list are stored in the database and returned "as is" to other featurs such as the search crawler, a dynamic field cannot be created.

It is possible to create a custom field that will display the value using todays date in its calculation.

  • Thanks Daniel, i read through some post suggesting you can do this using DataView webparts. Can it be helpful in my case? Will i be able to use the value in calculated columns? – Ishan Oct 28 '13 at 9:44
  • You could use a DataView webpart either XSLT or JavaScript to make a new column on your page (that won't ever exist as a "calculated column" as far as SharePoint knows) that includes today's date in its calculation. Generally, though, it's a whole lot easier to just convince your user to make do with filtered list views (which can use [Today]). Add two List View web parts to their page, filter one by [Whatever Date] <= [Today] and the other by [Whatever Date] > [Today]. – Aron Foster Oct 28 '13 at 19:08
0

I was looking to do this exact thing and came up with a workaround using javascript/jQuery and JSOM/REST on a Date/Time column rather than a calculated column in SPF 2013. I'm sure this code could be refactored to be more efficient, but I'm not a programmer (know enough to be dangerous though) and it is working for what I need it to do:

  • Create a Date/Time column named "Today" in the desired list
  • On the page that displays your list (e.g. http://siteName.com/Lists/ListName), add a Script Editor Web Part (or if you want to store your code in a .txt or .js file, a Content Editor Web Part linked to the file would work as well)
  • Insert the following code into your Script Editor Web Part (or .txt/.js file linked to a Content Editor Web Part) along with any necessary script tags (e.g. to execute this code, import jQuery libraries)

    _spBodyOnLoadFunctionNames.push("initPage");
    function initPage(){
    ExecuteOrDelayUntilScriptLoaded(updateTodayDate,'sp.js');
    }
    function updateTodayDate(){
    var $t = jQuery.noConflict(true);
    var requestURI = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('ListName')/Items";
    var itemID;
    var strdDt;
    var todayDt;
    var nd;
    var relodPg = false;

    $t.ajax({ url: requestURI, type: "GET", async: false, headers: { "ACCEPT": "application/json;odata=verbose" }, success: function(data){ itemID = data.d.results[0].ID var requestURI2 = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('ListName')/Items("+itemID+")"; $t.ajax({ url: requestURI2, type: "GET", async: false, headers: { "ACCEPT": "application/json;odata=verbose" }, success: function(data2){ var parseDt = data2.d.Today.split("T"); var dtParts = parseDt[0].split("-"); strdDt = (dtParts[0]*10000)+(dtParts[1]*100)+(dtParts[2]*1); nd = new Date(); todayDt = (nd.getFullYear()*10000)+((nd.getMonth()+1)*100)+(nd.getDate()*1); } });

    if( strdDt < todayDt ){ for(var i=0; i < data.d.results.length; i++){ var requestURI3 = _spPageContextInfo.webAbsoluteUrl + "/_api/web/Lists/GetByTitle('ListName')/Items("+data.d.results[i].ID+")"; var requestHeaders = { "X-HTTP-Method": "MERGE", "ACCEPT": "application/json;odata=verbose", "X-RequestDigest": $t("#__REQUESTDIGEST").val(), "If-Match": "*" } var itemData = { __metadata:{"type":"SP.Data.ListNameListItem"}, Today: (nd.getMonth()+1)+"/"+nd.getDate()+"/"+nd.getFullYear() } requestBody = JSON.stringify(itemData); $t.ajax({ url: requestURI3, type: "POST", async: false, contentType: "application/json;odata=verbose", headers: requestHeaders, data: requestBody, success: function(data3){ //alert("Updated item "+i); }, error: function(data3){ alert("Failed to update Today column"); } }); } relodPg = true; } if( relodPg ){ window.location.replace("http://siteName.com/Lists/ListName/AllItems.aspx") }; } }); }

Again, I'm sure this is more inefficient than it could be and only checking the date in the "Today" column on the first record could lead to records not being updated if someone has adjusted the value of the field manually. However, it runs at most one time per day (first person to visit the page) and it can be implemented without having the Administrator level access that would probably be required to implement other solutions like scheduling a Powershell script or creating a timer job.

Hopefully this helps someone else that is in a similar situation or it can lead to someone else more skilled than I to create a more elegant solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.