Search code examples
sharepoint-2013display-templates

Sharepoint: How to show AppendOnlyHistory on a display template in a cross-publishing scenario


The overarching requirement I am trying to implement is to show comments (made on a list, item by item basis).

I added the feature on the authoring side by enabling versioning on the list and adding a text field with the option "Append Changes to Existing Text" set to true. This indeed allows me to comment on items and displays them chronologically, but on the authoring side only. The issue is that the UI part will be done on another site collection and I can't find a straightforward way to get all comments there.

So far, every resource I have found points to

 <SharePoint:AppendOnlyHistory runat="server" FieldName="YourCommentsFieldName" ControlMode="Display"/>

The thing is, I can't (don't know how to) use this inside a display template. So far, I am getting all my data using the REST API, via

        var siteUrl=_spPageContextInfo.webAbsoluteUrl.replace("publishing","authoring");
        $.ajax({
            url: siteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items(" + id + ")",
            type: 'GET',
            async:false,
            headers: {"accept": "application/json;odata=verbose",},
            dataType: 'JSON',
            success: function(json) {
               console.log(json);
               //var obj = $.parseJSON(JSON.stringify(json.d.results));
               //alert(obj);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("error :"+XMLHttpRequest.responseText);
            }
        });

What this gives me is the latest comment only. I need a simple way to get a hold of the entire thread.


Solution

  • I ended up using javascript object model to get them like so:

    function GetComments(listname, itemId) {
        var siteUrl = _spPageContextInfo.webAbsoluteUrl.replace("publishing", "authoring");
        if ($(".comments-history").length) {
            $().SPServices({
                operation: "GetVersionCollection",
                async: false,
                webURL: siteUrl,
                strlistID: listname,
                strlistItemID: itemId,
                strFieldName: "Comments",
                completefunc: function (xData, Status) {
    
                    $(xData.responseText).find("Version").each(function (data, i) {
    
                        var xmlComment = $(this)[0].outerHTML;
                        var arr = xmlComment.split(/comments|modified|editor/g);
                        var comment = arr[1].trim().substring(2, arr[1].length-2);
                        var dateSt = Date.parse((arr[2].substring(1, arr[2].length)).replace('/"', ''));
                        var user = getUsername(arr[3]);
    
                        var st = "<div class='comment-item'><div class='comment-user'>" + user + "(" + FormatDate(dateSt) + ")</div>";
                        st += "<div class='comment-text'>" + comment + "</div></div>";
                        $(".comments-history").append(st);
                    });
                }
            });
        }
    }
    

    the parsing could be better, but this is just an initial working idea