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!!

  • 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
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,

  • 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 8: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
  • 3) 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: msdn.microsoft.com/en-us/library/ie/cd9w2te4(v=vs.94).aspx – John-M Nov 17 '14 at 14:16

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.