0

I am trying to do the following modifications to my display template:-

1- If the item created date = today or yesterday

2- To attach the following to the title, to show the built-in new icon.

<span class="ms-newdocument-iconouter">
<img class="ms-newdocument-icon" src="/_layouts/15/images/spcommon.png?rev=23" alt="new" title="new">
</span>

Now the problem i am facing is that the Created managed property will return its value as follow Thursday, October 13, 2016

so i tried adding the following check inside my display template as a test :-

 <!--#_

                    if ($getItemValue(ctx, "Created").value === "Thursday, October 13, 2016")
        {

                    <span class="ms-newdocument-iconouter">
<img class="ms-newdocument-icon" src="/_layouts/15/images/spcommon.png?rev=23" alt="new" title="new">
</span>
}
                     _#-->

but i got an exception

Template '~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Item_List_Item.js' not found or has syntax errors. (LoadTemplate: ~sitecollection/_catalogs/masterpage/Display Templates/Content Web Parts/Group_Content.js)

so can anyone adivce more on my above 2 point please?

1

You need to compare the date format just like the way you do in javascript.

var createdDate = new Date((ctx.CurrentItem.Created)); 
if(createdDate !=null){
    createdDate = createdDate.format("dd/MM/yyyy");
}
var today = new Date();
today = today.format("dd/MM/yyyy");

<!--#_

if (createdDate == today)
{
  var newIconHtml = '<span class="ms-newdocument-iconouter"><img class="ms-newdocument-icon" src="/_layouts/15/images/spcommon.png?rev=23" alt="new" title="new"/></span>'
}

_#-->

after that find your Title html and append the above html/javascript code like:

_#= newIconHtml =#_
  • OK your appraoch worked well for me.. i will mark it answer.. but can you advice if i can modify this condition "if (createdDate == today)" to allow showing the new icon if the created date equal today or yesterday or the day before yesterday... so the new icon will be shown if the item was created at-least three days ago? – john Gu Oct 15 '16 at 1:31
0

Try to debug your displaytemplate. That will help you understand what is going wrong. Sadly all errors are caught when the displaytemplate is executed, so the error you get could mean anything.

My typical troubleshooting goes

  1. Is the newest version of my displaytemplate aviable (with the browser dev tools)
  2. Debug my displaytemplate to find the error (with the browser dev tools)
  • the issue is that inside the display template i will be dealing with managed properties and not real columns so i am not sure if ".ifnew" function is available inside the managed property names.. – john Gu Oct 13 '16 at 14:44
  • @johnG oh well I messed up - sharepoint does this for lists and my answer would work in JSLink but thats obviously not what you need. I wrote an update to my answer. – Mx. Oct 13 '16 at 14:57

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.