Question

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

Was it helpful?

Solution

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,

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top