1

I need the modified date of the item when search results displayed. kindly help me doing this. I referred some MSDN article, where they have mentioned editing HTML file of display template on "Design galleries" under site settings. :- kindly help me out with HTML code to get it or any other step to do it.

My requirement Web part settings details

HTML file of my Item display template. Default Item

<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
<mso:MasterPageDescription msdt:dt="string">Displays the default result item template.</mso:MasterPageDescription>
<mso:ContentTypeId msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId>
<mso:TargetControlType msdt:dt="string">;#SearchResults;#</mso:TargetControlType>
<mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
<mso:ManagedPropertyMapping msdt:dt="string">&#39;Title&#39;:&#39;Title&#39;,&#39;Path&#39;:&#39;Path&#39;,&#39;Description&#39;:&#39;Description&#39;,&#39;EditorOWSUSER&#39;:&#39;EditorOWSUSER&#39;,&#39;LastModifiedTime&#39;:&#39;LastModifiedTime&#39;,&#39;CollapsingStatus&#39;:&#39;CollapsingStatus&#39;,&#39;DocId&#39;:&#39;DocId&#39;,&#39;HitHighlightedSummary&#39;:&#39;HitHighlightedSummary&#39;,&#39;HitHighlightedProperties&#39;:&#39;HitHighlightedProperties&#39;,&#39;FileExtension&#39;:&#39;FileExtension&#39;,&#39;ViewsLifeTime&#39;:&#39;ViewsLifeTime&#39;,&#39;ParentLink&#39;:&#39;ParentLink&#39;,&#39;FileType&#39;:&#39;FileType&#39;,&#39;IsContainer&#39;:&#39;IsContainer&#39;,&#39;SecondaryFileExtension&#39;:&#39;SecondaryFileExtension&#39;,&#39;DisplayAuthor&#39;:&#39;DisplayAuthor&#39;</mso:ManagedPropertyMapping>
<mso:HtmlDesignConversionSucceeded msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>
<mso:HtmlDesignStatusAndPreview msdt:dt="string">https://itg-proposalweb.ext.hp.com/_catalogs/masterpage/Display%20Templates/Search/Item_Default.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>
<mso:_dlc_DocId msdt:dt="string">TKEDMPT7QM5T-128-240</mso:_dlc_DocId>
<mso:_dlc_DocIdItemGuid msdt:dt="string">c172ec75-6a31-49db-a278-d7fc3ce31c84</mso:_dlc_DocIdItemGuid>
<mso:_dlc_DocIdUrl msdt:dt="string">https://itg-proposalweb.ext.hp.com/_layouts/15/DocIdRedir.aspx?ID=TKEDMPT7QM5T-128-240, TKEDMPT7QM5T-128-240</mso:_dlc_DocIdUrl>
</mso:CustomDocumentProperties>
</xml><![endif]-->
</head>
<body>
    <div id="Item_Default">
<!--#_ 
        if(!$isNull(ctx.CurrentItem) && !$isNull(ctx.ClientControl)){
            var id = ctx.ClientControl.get_nextUniqueId();
            var itemId = id + Srch.U.Ids.item;
            var hoverId = id + Srch.U.Ids.hover;
            var hoverUrl = "~sitecollection/_catalogs/masterpage/Display Templates/Search/Item_Default_HoverPanel.js";
            $setResultItem(itemId, ctx.CurrentItem);
            if(ctx.CurrentItem.IsContainer){
                ctx.CurrentItem.csr_Icon = Srch.U.getFolderIconUrl();
            }
            ctx.currentItem_ShowHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);
            ctx.currentItem_HideHoverPanelCallback = Srch.U.getHideHoverPanelCallback();
_#-->
            <div id="_#= $htmlEncode(itemId) =#_" name="Item" data-displaytemplate="DefaultItem" class="ms-srch-item" onmouseover="_#= ctx.currentItem_ShowHoverPanelCallback =#_" onmouseout="_#= ctx.currentItem_HideHoverPanelCallback =#_">
                _#=ctx.RenderBody(ctx)=#_
                <div id="_#= $htmlEncode(hoverId) =#_" class="ms-srch-hover-outerContainer"></div>
            </div>
<!--#_ 
        } 
_#-->
    </div>
</body>
</html>
1
<!--[if gte mso 9]><xml>
 <mso:CustomDocumentProperties>
 <mso:TemplateHidden msdt:dt="string">0</mso:TemplateHidden>
 <mso:MasterPageDescription msdt:dt="string">Displays the default result 
  item template.</mso:MasterPageDescription>
 <mso:ContentTypeId 
 msdt:dt="string">0x0101002039C03B61C64EC4A04F5361F385106603
 </mso:ContentTypeId>
 <mso:TargetControlType msdt:dt="string">;#SearchResults;# 
 </mso:TargetControlType>
 <mso:HtmlDesignAssociated msdt:dt="string">1</mso:HtmlDesignAssociated>
 <mso:ManagedPropertyMapping 
 msdt:dt="string">&#39;Title&#39;:&#39;Title&#39;,&#39;Path&#39;:&#39
 ;Path&#39;,&#39;Description&#39;:&#39;Description&#39;,&#39;EditorOWSUSER
 &#39;:&#39;EditorOWSUSER&#39;,&#39;LastModifiedTime&#39;:&#39;
 LastModifiedTime&#39;,&#39;CollapsingStatus&#39;:&#39;
 CollapsingStatus&#39;,&#39;DocId&#39;:&#39;DocId&#39;,&#39;
 HitHighlightedSummary&#39;:&#39;HitHighlightedSummary&#39;,&#39;
 HitHighlightedProperties&#39;:&#39;HitHighlightedProperties&#39;,&#39;
 FileExtension&#39;:&#39;FileExtension&#39;,&#39;ViewsLifeTime&#39;:&#39;
 ViewsLifeTime&#39;,&#39;ParentLink&#39;:&#39;ParentLink&#39;,&#39;
 FileType&#39;:&#39;FileType&#39;,&#39;IsContainer&#39;:&#39;
 IsContainer&#39;,&#39;SecondaryFileExtension&#39;:&#39;
 SecondaryFileExtension&#39;,&#39;DisplayAuthor&#39;:&#39;
 DisplayAuthor&#39;</mso:ManagedPropertyMapping>
 <mso:HtmlDesignConversionSucceeded 
 msdt:dt="string">True</mso:HtmlDesignConversionSucceeded>
 <mso:HtmlDesignStatusAndPreview msdt:dt="string">https://itg- 
proposalweb.ext.hp.com/_catalogs/masterpage/Display%20Templates/Search/
Item_Default.html, Conversion successful.</mso:HtmlDesignStatusAndPreview>
<mso:_dlc_DocId msdt:dt="string">TKEDMPT7QM5T-128-240</mso:_dlc_DocId>
<mso:_dlc_DocIdItemGuid msdt:dt="string">c172ec75-6a31-49db-a278- 
d7fc3ce31c84</mso:_dlc_DocIdItemGuid>
<mso:_dlc_DocIdUrl msdt:dt="string">https://itg- 
proposalweb.ext.hp.com/_layouts/15/DocIdRedir.aspx?ID=TKEDMPT7QM5T-128-240, 
TKEDMPT7QM5T-128-240</mso:_dlc_DocIdUrl>
</mso:CustomDocumentProperties>
</xml><![endif]-->
</head>
<body>
<div id="Item_Default">
<!--#_ 
  if(!$isNull(ctx.CurrentItem) && !$isNull(ctx.ClientControl)){
    var id = ctx.ClientControl.get_nextUniqueId();
    var itemId = id + Srch.U.Ids.item;
    var hoverId = id + Srch.U.Ids.hover;
    var hoverUrl = "~sitecollection/_catalogs/masterpage/Display 
    Templates/Search/Item_Default_HoverPanel.js";
    $setResultItem(itemId, ctx.CurrentItem);
    if(ctx.CurrentItem.IsContainer){
        ctx.CurrentItem.csr_Icon = Srch.U.getFolderIconUrl();
    }
    ctx.currentItem_ShowHoverPanelCallback = 
    Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl);
    ctx.currentItem_HideHoverPanelCallback = 
    Srch.U.getHideHoverPanelCallback();
_#-->
    <div id="_#= $htmlEncode(itemId) =#_" name="Item" data- 
    displaytemplate="DefaultItem" class="ms-srch-item" onmouseover="_#= 
    ctx.currentItem_ShowHoverPanelCallback =#_" onmouseout="_#= 
    ctx.currentItem_HideHoverPanelCallback =#_">
        _#=ctx.RenderBody(ctx)=#_
        <div id="_#= $htmlEncode(hoverId) =#_" class="ms-srch-hover- 
        outerContainer"></div>
    </div>
 <!--#_ 
   }
 if (ctx.CurrentItem.LastModifiedTime) {
        var lastModifiedTime = ctx.CurrentItem.LastModifiedTime;
        var lastModifiedMonth = lastModifiedTime.getMonth() + 1;
        if(lastModifiedMonth < 10) {lastModifiedMonth = "0" + 
        lastModifiedMonth}
        var lastModifiedDate = lastModifiedTime.getDate();
        if(lastModifiedDate < 10) {lastModifiedDate = "0" + 
        lastModifiedDate}
        var lastModifiedTimeHtml = lastModifiedTime.getFullYear() + "-" 
        + lastModifiedMonth + "-" + lastModifiedDate;
  _#-->
  <div class="srch-subTitle">
  <h3 class="ms-soften" style="font-size:14px; display:inline">_#= 
  $htmlEncode(Srch.Res.hp_LastModified) =#_</h3><span class="ms-srch- 
  ellipsis">: _#= $htmlEncode(lastModifiedTimeHtml) =#_</span>
  </div>
 <!--#_ 
    }
 _#-->
</div>
</body>
</html>
  • @Chetan I hope this answer help you to achive your goal – Melad Batta Apr 11 '18 at 13:37
  • I edited my file with your code. But when I select this edited or any other display template from web part setting my page will be loading when I try to click apply button – Chetan Apr 12 '18 at 7:10
  • @Chetan You can do this in a different way, in site settings go to Searh Result Types and there you can select the display template you want to link to your result type – Melad Batta Apr 12 '18 at 7:17
  • I did as you said, but my changes are not reflecting on the search page.Then saw that in the above-attached web part settings screenshot we selected "Use result type to display items" radio button. So I need to change that result type file right? – Chetan Apr 12 '18 at 10:40
  • No, you don't need to do that' – Melad Batta Apr 12 '18 at 10:43
1

I got the output what i expected now i am getting Date Modified in the search result. I edited "Result type" in setting and created custom word item display template by adding HTML code to that file.

To Get Date&Time: var modifiedDate= ctx.CurrentItem.LastModifiedTime This will get the date and time in UTC format like Sat Jul 19 2014 11:51:22 GMT+0530 (India Standard Time)

If you want only Date, use $getItemValue() as var modifiedDate= $getItemValue(ctx, "LastModifiedTime") This will give Time like Saturday, July 19, 2014

Now once you have both the values, you can display in any div or create your own html element and add it below the ctx.RenderBody(ctx) call.

Modified by,_#= author =#_ On _#= modifiedDate=#_

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.