0

I had no luck with a solution for the [Today] Problem so far, pretty much all the solutions on the web have a catch. So I thought, why not use the Modified field instead of Today? All that is needed is to keep updating the items daily. I want to avoid the workflow Loop, thus the best solution seems to include a Script Editor Web Part, that will update the column for all items (let's call it "ColumnUpdate") every time the page is refreshed. So the idea is every time the user opens the page with the list, the script will update the ColumnUpdate for all items, thus Modified is set to today, thus calculated fields (eg "due days") are updated.

I tried to figure out the script, but I'm not very familiar with JavaScript and pretty much nothing succeeded so far.

This is the last one I tried:

<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript">   </script> 
<script type="text/javascript">

$("input[title='ColumnUpdate']").val('abc');

</script>

Any help will be highly appreciated!!

Amal Hashim
  • 28,306
  • 5
  • 31
  • 61
ConAchilleos
  • 310
  • 1
  • 8
  • 1
    Are you asking how to update a field ColumnUpdate with some value on Page Load or Refresh? – Amal Hashim Nov 12 '14 at 13:34
  • Exactly, I want it to be updated on Page Load or refresh for all items, I don't care what value it takes, just to keep the Modified field to current date. – ConAchilleos Nov 12 '14 at 13:36
  • Which version of SharePoint you are using? – Amal Hashim Nov 12 '14 at 13:37
  • 1
    Could you explain some of your underlying calculations (due days, for example) to clarify what you're trying to achieve? I think it would be an extreme case of overkill to update every single list item to make a calculation work, and I suspect there is likely an easier way to accomplish what you want. – John-M Nov 12 '14 at 13:47
  • We are using SharePoint Foundation 2013 - we are currently developing a test solution, for the company to decide if they should invest in SharePoint Server 2013. – ConAchilleos Nov 12 '14 at 13:48
  • For example for the [Next Due] calculated column (which returns a number of 0 decimal places)- ([Next Due Date] is a simple Date column): =ROUNDUP(([Next Due Date]-[Modified]);0) The list will never hold more than 22 items. – ConAchilleos Nov 12 '14 at 13:51
  • Do you need to store the 'Next Due' value in the SharePoint list for some particular reason, or would it be sufficient to have it calculated on page load for display only? The need to store this (constantly changing) value in the list is problematic; chiefly because it needs updated all the time, but also because you would need to delay displaying them until all the updates were completed (and make sure the user waits for the updates to complete before closing the window for example), which makes an out of the box list view significantly more difficult to work with. – John-M Nov 12 '14 at 14:03
  • The whole idea behind this is for the Accounting department to view all of the due/overdue Dates collectively on one list. The requested to view the next due date, and as well how many day till the next due ( this is [Next Due] ). So [Next Due] is only for dispaly. Everything is set just missing the dynamic Today field, and this seems to be the "best" Workaround so far. – ConAchilleos Nov 12 '14 at 14:10

1 Answers1

1

Edit to handle Minimal Download Strategy (MDS) which is turned on by default

If you don't need to store the Next Due value I would just use Client-Side Rendering to generate it on each view and render it to your users.

I have an answer to another question that is quite similar to the situation I'm proposing, you can find it here: Days Past Since List Item Created - SharePoint Online 2013

To summarize and provide some instructions specific to your situation though:

  • Create the Next Due field in your list. The type and value don't actually matter, we're just going to use it as a place-holder.
  • Create a new JS file (I named mine jsLinkTest.js) and store it in somewhere like an Asset Library (I use SharePoint Online, so I don't have access to put stuff in _layouts, or anything like that -- plus using an Asset Library just makes the approach less complex)

Here is the code for the JS file:

// SharePoint Online / 2013 enables MDS by default, so we'll setup our 
// custom rendering to play nicely
RegisterModuleInit('/<yourSiteName>/SiteAssets/jsLinkTest.js', registerDueDateCalc);
// this will call the rendering function though on first load or when MDS doesn't apply
registerDueDateCalc();

function registerDueDateCalc() {
    // the name for this object doesn't matter
    var timeSinceFieldViewCtx = {};
    // just use the Templates and Templates.Fields convention below
    timeSinceFieldViewCtx.Templates = {};
    timeSinceFieldViewCtx.Templates.Fields = {
        // Each key value in this object should be the field name
        // so spelling and case matter and should match your list
        "Next_x0020_Due": {
            /* Each key here should be the view/form you want to use
               this custom rendering, choices include: 'View', 'DisplayForm',
               'EditForm', 'NewForm'. The value should point to the function 
               that will actually perform the rendering, which we will define 
               below -- outside of this function. */ 
            "View": timeSinceFieldViewTemplate
        }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(timeSinceFieldViewCtx);
}

function timeSinceFieldViewTemplate(ctx) {
    // you access the current list item properties with ctx.CurrentItem
    var dateDiff = new Date(ctx.CurrentItem.Next_x0020_Due_x0020_Date) - new Date();
    var daysDiff = Math.floor(dateDiff / 1000 / 60 / 60 / 24);
    return daysDiff + " Days Until Due";
}
  • Save the file, and copy a link to it (make sure to replace the <yoursite> placeholder in the file path with your actual site name).
  • Go to the list view that you want to apply this rendering to; edit the page (you may need to look in the dropdown under the gear); edit the list view web part. Under 'Miscellaneous' you'll see the "JS Link" property.

Something that I got caught up on, is that you can't just use a normal URL here as the link property, you have to use a URL token like the ones listed here. Like I said earlier, I put my file in the Assets Library, and I called it jsLinkTest.js so my JS Link value was:

~site/SiteAssets/jsLinkTest.js

That should get you headed in the right direction, you can adjust the timeSinceFieldViewTemplate function if you'd like as well...

Notice the field names use the _x0020_ encoding for spaces, since this is how SharePoint handles spaces internally on field names.

If you're still set on updating every list item, you'll find it's not terribly difficult to perform the actual updates, you just introduce a bunch of other issues as I mentioned in comments to your question.

A possible downside of this approach is that you can't directly sort or filter on this custom rendered column, you would need to do that on the actual due date column, but you can use today in view filters or sort on the due date field directly.

Hope this helps,

John-M
  • 5,930
  • 3
  • 18
  • 36
  • Thank you for your time and answer!! I'm trying it at the moment but unfortunately the Next Due column remains empty. I'll keep working on it and hopefully will get it soon. Also I have a JS "newbie" question: On this line: var dateDiff = new Date(ctx.CurrentItem.Next_x0020_Due_x0020_Date) - new Date(); You assign the var dateDiff to a new object of type date, which take the value of Next Due Date (so far so good) and the subtract a new object of type date, which is empty. The question is, does the empty object takes Today's date by Default? Thank you again!! – ConAchilleos Nov 17 '14 at 08:01
  • Your solution should have worked. I have tried different CSR solutions just for testing and none of them seem to take any action on the site. So the problem is not with your solution but something else from my part. I've been investigating what could be "missing" but not successful so far... This is a bit weird – ConAchilleos Nov 17 '14 at 10:24
  • Another Update:
    1. Seems you forgot a '(' before the funstion in the code above
    2. I'm getting SCRIPT5009: 'registerDueDateCalc' is undefined, when I refresh the page. So now that I know the Problem, we are heading somewhere :D
    – ConAchilleos Nov 17 '14 at 12:07
  • Commended out the first two lines of code and (lines 3 & 5) and on line 7 I simply switched to (function(){. Now it runs but the field displays NaN
  • – ConAchilleos Nov 17 '14 at 12:17
  • And in answer to the first part: yes, a new Date() object takes the current system date and time and creates a new Date object with those values. You can pass all sorts of date strings in to the constructor as well to get other dates, a fairly good reference is here: http://msdn.microsoft.com/en-us/library/ie/cd9w2te4(v=vs.94).aspx – John-M Nov 17 '14 at 14:16
  • Thank you once again!! Unfortunately you guessed correctly the static name :) It is Next_x0020_Due_x0020_Date and therefore the reason I get NaN lies somewhere else. I checked Next Due and it's also correct, tried changing it to date&time just in case but still I get NaN. – ConAchilleos Nov 17 '14 at 14:40
  • The column is now blank. Debugging shows no Errors, so I guess Next Due Date is blank? But it's not. It's of time&date Format should it be single line of text? – ConAchilleos Nov 17 '14 at 15:04
  • If i Switch var dateDiff = new Date(ctx.CurrentItem.Next_x0020_Due_x0020_Date) - new Date(); to var dateDiff = new Date(ctx.CurrentItem.Next_x0020_Due_x0020_Date); dateDiff =- new Date(); Next Due Displays -16392. Dunno if this helps. Having dateDiff= new Date(); will display 16392. Maybe it's because I'm using german date Format? – ConAchilleos Nov 17 '14 at 15:25
  • ok figured it out! Changed the lacele (date Format) in the regional Setting first to English (UK). That got rid of NaN but gave the wrong number, apparently JS would not recognize the German Format (dd.mm.yyyy), probably Needs '/' to read Dates. Anyway I tried the English (USA) Format (mm/dd/yyyy) and I get the correct number! – ConAchilleos Nov 18 '14 at 08:13
  • Thanks a lot for all the help and the time spend trying to figure out the solution and "Debugging" with me :) Honestly this is higly appreciated! I will make a lot of use of this, you opened many doors for me! But first I'll switch back to the german locale (so users don't complain) and rearange the date once input in the script to match the correct format. Thank you again!! – ConAchilleos Nov 18 '14 at 08:13
  • Great to hear -- glad that got it working for you! You should be able to map back and forth to the right date formats now that you know what's going on under there -- someone in a different answer suggested moment.js as a possible helper for working with dates in js may be worth checking out! – John-M Nov 18 '14 at 14:02