Sharepoint - Display Template - link blog post comment to related blog post
Comments list stores PostTitle which in turn contains both ID and Title for the Post. In the ViewComments.aspx page, you can use REST api to create a link to the related Post.
var listname = 'Comments';
JSRequest.EnsureSetup(); // or use other function to retrieve param
var id = JSRequest.QueryString["ID"];
//REST call
var call = $.ajax({
url: _spPageContextInfo.webAbsoluteUrl
+ "/_api/web/lists/getbytitle('" + listname + "')/items(" + id + ")"
+ "?$select=PostTitle/Id,PostTitle/Title"
+ "&$expand=PostTitle",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data) {
//window.console && console.log(data);
var response = data.d;
var subMenuAnchor = $('<a/>', {
href: _spPageContextInfo.webAbsoluteUrl + "/Lists/Posts/Post.aspx?ID=" + response.PostTitle.Id,
html: response.PostTitle.Title
});
var relatedItemHeader = $('<h3/>', {
html: subMenuAnchor
});
//Find an element on the ViewComments page to append the result
relatedItemHeader.appendTo(document.getElementById('MSOZoneCell_WebPartWPQ1'));
});
call.fail(function (jqXHR, textStatus, errorThrown) {
alert("Error: " + jqXHR.responseText);
});
By default there is no managed property for comments' parent PostId and PostTitle, thus they're not retrievable via search.
You have to manually create a managed property "PostId" and map the crawled property "ows_PostID". Start a full crawl. Now the "PostId" property is included with every blog comment you retrieve via search. (The same goes for "ows_PostTitle" in case you need it.)