Hello Interwebs!
I have been doing a ton of search work lately and just
wanted to share a useful script for tracking down those pesky Managed Property
Values. I have been wanting to write a
search series, but just can’t seem to find the time to put pen to paper (or
fingers to keyboard in my case) to get them written up!
When I first started working with search I think my biggest
headache was to tell which managed properties were being retrieved and what the
values were for each managed property.
The below script can be added to your display template to log the
managed property name & value to your console window for each search result
returned.
for (var p in ctx.CurrentItem) { console.log(p + ":" + $getItemValue(ctx, p)); }
This code can be implemented within any display template,
but for this example I will be using the Item_CommonItem_Body display template
(NOTE after your
test you should remove the code snippet due to compatibility issues for some
browser versions & that there really isn’t a reason to log these values to
console outside of testing) I am using this particular template as an example,
but any display template which is called from search could be used.
Below is the default code from Body tag of our
Item_Common_Body display template where I have added the script to iterate
through the managed properties.
<body> <div id="Item_CommonItem_Body"> <!--#_ var id = ctx.CurrentItem.csr_id; var title = Srch.U.getHighlightedProperty(id, ctx.CurrentItem, "Title"); if ($isEmptyString(title)) {title = $htmlEncode(ctx.CurrentItem.Title)} var useWACUrl = !$isEmptyString(ctx.CurrentItem.ServerRedirectedURL); if(ctx.ScriptApplicationManager && ctx.ScriptApplicationManager.states){ useWACUrl = (useWACUrl && !ctx.ScriptApplicationManager.states.openDocumentsInClient); } var appAttribs = ""; if(!useWACUrl) { if (!$isEmptyString(ctx.CurrentItem.csr_OpenApp)) { appAttribs += "openApp=\"" + $htmlEncode(ctx.CurrentItem.csr_OpenApp) + "\"" }; if (!$isEmptyString(ctx.CurrentItem.csr_OpenControl)) { appAttribs += " openControl=\"" + $htmlEncode(ctx.CurrentItem.csr_OpenControl) + "\"" }; } var showHoverPanelCallback = ctx.currentItem_ShowHoverPanelCallback; if (Srch.U.n(showHoverPanelCallback)) { 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"; showHoverPanelCallback = Srch.U.getShowHoverPanelCallback(itemId, hoverId, hoverUrl); } var displayPath = Srch.U.getHighlightedProperty(id, ctx.CurrentItem, "Path"); if ($isEmptyString(displayPath)) {displayPath = $htmlEncode(ctx.CurrentItem.Path)} var url = ctx.CurrentItem.csr_Path; if($isEmptyString(url)){ if(useWACUrl) { url = ctx.CurrentItem.ServerRedirectedURL; } else { url = ctx.CurrentItem.Path; } } ctx.CurrentItem.csr_Path = url; var pathLength = ctx.CurrentItem.csr_PathLength; if(!pathLength) {pathLength = Srch.U.pathTruncationLength} var maxTitleLengthInChars = Srch.U.titleTruncationLength; var termsToUse = 2; if(ctx.CurrentItem.csr_PreviewImage != null) { maxTitleLengthInChars = Srch.U.titleTruncationLengthWithPreview; termsToUse = 1; } var clickType = ctx.CurrentItem.csr_ClickType; if(!clickType) {clickType = "Result"} _#--> <div id="_#= $htmlEncode(id + Srch.U.Ids.body) =#_" class="ms-srch-item-body" onclick="_#= showHoverPanelCallback =#_"> <!--#_ if (!$isEmptyString(ctx.CurrentItem.csr_Icon)) { _#--> <div class="ms-srch-item-icon"> <img id="_#= $htmlEncode(id + Srch.U.Ids.icon) =#_" onload="this.style.display='inline'" src="_#= $urlHtmlEncode(ctx.CurrentItem.csr_Icon) =#_" /> </div> <!--#_ } var titleHtml = String.format('<a clicktype="{0}" id="{1}" href="{2}" class="ms-srch-item-link" title="{3}" onfocus="{4}" {5}>{6}</a>', $htmlEncode(clickType), $htmlEncode(id + Srch.U.Ids.titleLink), $urlHtmlEncode(url), $htmlEncode(ctx.CurrentItem.Title), showHoverPanelCallback, appAttribs, Srch.U.trimTitle(title, maxTitleLengthInChars, termsToUse)); } _#--> <div id="_#= $htmlEncode(id + Srch.U.Ids.title) =#_" class="ms-srch-item-title"> <h3 class="ms-srch-ellipsis"> _#= titleHtml =#_ </h3> </div> <!--#_ if (!$isEmptyString(ctx.CurrentItem.HitHighlightedSummary)) { _#--> <div id="_#= $htmlEncode(id + Srch.U.Ids.summary) =#_" class="ms-srch-item-summary">_#= Srch.U.processHHXML(ctx.CurrentItem.HitHighlightedSummary) =#_</div> <!--#_ } var truncatedUrl = Srch.U.truncateHighlightedUrl(displayPath, pathLength); _#--> <div id="_#= $htmlEncode(id + Srch.U.Ids.path) =#_" tabindex="0" class="ms-srch-item-path" title="_#= $htmlEncode(ctx.CurrentItem.Path) =#_" onblur="Srch.U.restorePath(this, '_#= $scriptEncode(truncatedUrl) =#_', '_#= $scriptEncode(ctx.CurrentItem.Path) =#_')" onclick="Srch.U.selectPath('_#= $scriptEncode(ctx.CurrentItem.Path) =#_', this)" onkeydown="Srch.U.setPath(event, this, '_#= $scriptEncode(ctx.CurrentItem.Path) =#_', '_#= $scriptEncode(truncatedUrl) =#_')" > _#= truncatedUrl =#_ </div> </div> <!--#_ if (!$isEmptyString(ctx.CurrentItem.csr_PreviewImage)) { var altText = Srch.Res.item_Alt_Preview; if(!$isEmptyString(ctx.CurrentItem.csr_PreviewImageAltText)){ altText = ctx.CurrentItem.csr_PreviewImageAltText; } var onloadJS = "var container = $get('" + $scriptEncode(id + Srch.U.Ids.preview) + "'); if(container){container.style.display = 'inline-block';}" + "var path = $get('" + $scriptEncode(id + Srch.U.Ids.path) + "'); if (path) { Srch.U.ensureCSSClassNameExist(path, 'ms-srch-item-preview-path');}" + "var body = $get('" + $scriptEncode(id + Srch.U.Ids.body) + "'); if (body) { Srch.U.ensureCSSClassNameExist(body, 'ms-srch-item-summaryPreview');}"; var previewHtml = String.format('<a clicktype="{0}" href="{1}" class="ms-srch-item-previewLink" {2}><img class="ms-srch-item-preview" src="{3}" alt="{4}" onload="{5}" /></a>', $htmlEncode(clickType), $urlHtmlEncode(url), appAttribs, $urlHtmlEncode(ctx.CurrentItem.csr_PreviewImage), $htmlEncode(altText), onloadJS); _#--> <div id="_#= $htmlEncode(id + Srch.U.Ids.preview) =#_" class="ms-srch-item-previewContainer"> _#= previewHtml =#_ </div> <!--#_ } for (var p in ctx.CurrentItem) { console.log(p + ":" + $getItemValue(ctx, p)); } _#--> </div> </body>
Now that we have added the script, saved and published the display
template we are ready to see what is brought back from our search results!
- Open your browser’s developer tools
- i.e. FireBug, Chrome Tools, F12, etc.
- Enter a search query into the search box which will return results
- You can use * to grab everything
- Select the console tab in your developer tools
- Bask in the awesome... There are actually more properties for this particular item than the screenshot shows, but I can only grab so many with a screen capture :)
Hope This Helps!
Dan