Marking search results that are inplace records with a different look/icon
-
12-12-2020 - |
Question
We're using inplace records as a way of declaring news and guides out-of-date. I really want to be able to display it for users when they search and a search result is out-of-date (record).
On the news itself, I've used the vtiItemDeclaredRecordOWSDATE
managed property to style the page differently marking it out-of-date.
But when I use a display template for my search results, the managed property vtiItemDeclaredRecordOWSDATE
is not exposed to the display template. I've added it as a ManagedPropertyMapping
of course.
If I do a REST call to: <mysite>/_api/search/query?querytext='MyQuery'&selectproperties='vtiItemDeclaredRecordOWSDATE'
the column is displayed but under the heading: SP.SimpleDataTable
Is there any way to retrieve the vtiItemDeclaredRecordOWSDATE
from within my Display Template?
La solution
So I managed to solve this.
In the display template, I call a function placed in another js-file with the element's path as the only attribute. This function then find context and builds a REST url based on the path, retrieving only the vtiItemDeclaredRecordOWSDATE.
Then it checks if there is actually a value in that attribute and if so, finds the element again with jQuery and sets the appropriate styling.
function getItemContext(path) {
var clientContext;
$(document).ready(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', execOperation);
});
function execOperation() {
try {
clientContext = new SP.ClientContext.get_current();
getMoreOnItem(clientContext, path);
}
catch (err) {
console.log(err);
}
}
}
function getMoreOnItem(clientContext, path) {
var myPath = "http://sc.ccta.dk/_api/search/query?querytext='* path=\"" + path + "\"'&selectproperties='vtiItemDeclaredRecordOWSDATE'";
$.ajax({
url: myPath,
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
findRecord(data, path);
},
error: function (data) {
console.log("Fejl" + data);
}
});
}
function findRecord(data, path) {
if (data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results.length > 0) {
var recordDataArray = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results[0].Cells.results;
for (i = 0; i < recordDataArray.length ;i++) {
if (recordDataArray[i].Key == "vtiItemDeclaredRecordOWSDATE") {
if (recordDataArray[i].Value !== null) {
console.log("Denne er posteret!");
$(".SKATWebItem a[href='" + path+ "']").parent().parent().parent().css("background-color", "#DADADA");
$(".SKATWebItem a[href='" + path+ "']").parent().parent().parent().css("padding", "10px");
$(".SKATWebItem a[href='" + path+ "']").parent().parent().parent().prepend("<div class='SKAThistorisk'>Denne meddelelse er historisk og ikke længere gældende</div>");
$(".SKATWebItem a[href='" + path+ "']").parent().parent().css("padding-left", "30px");
$(".SKATWebItem a[href='" + path+ "']").parent().parent().next().css("padding-left", "30px");
$(".SKATWebItem a[href='" + path+ "']").parent().parent().next().next().css("padding-left", "30px");
}
///console.log(recordDataArray[i].Value);
}
}
}
}