Dec 18 2008 12

Separate trackbacks and comments (and do it in BlogEngine.NET)

It could be really annoyng when you try to read comments on a blog post with large number of trackbacks. To allow your readers better user experience you can separate trackback from your comments. This short tutorial will show you how to do it in Blogengine.NET quickly (and dirty).

The key is in CommentView.ascx located in \User controls\ subfolder. In original BlogEngine.NET version you have a placeholder for Comments. We are going to add one more - for trackbacks.

<div id="commentlist" style="display:none">
  <asp:PlaceHolder runat="server" ID="phComments" />
</div>
<asp:PlaceHolder runat="server" ID="phTrckbacks" />

And we have to modify code behind. Here is a short description: when adding approved comments we have to ommit trackbacks by excluding all items that contains "pingback" or "trackback" word in Email property. Quite simple. Now we have to add one moreforeach loop and use the same condition to add trackback to newly added placeholder. Take a look at the code below.

string pingbackPath = Utils.RelativeWebRoot + "themes/" + theme + "/PingBackView.ascx";

//Add approved Comments
foreach (Comment comment in Post.Comments)
{
     CommentViewBase control = (CommentViewBase)LoadControl(path);
     if ((comment.IsApproved || !BlogSettings.Instance.EnableCommentsModeration) && (comment.Email != "pingback" && comment.Email != "trackback"))
     {
          control.Comment = comment;
          control.Post = Post;
          phComments.Controls.Add(control);
     }
}

//Add unapproved comments
foreach (Comment comment in Post.Comments)
{
     CommentViewBase control = (CommentViewBase)LoadControl(path);

     if (!comment.IsApproved && Page.User.Identity.IsAuthenticated)
     {
          control.Comment = comment;
          control.Post = Post;
          phComments.Controls.Add(control);
     }
}

//add trackbacks
Panel pnlPingbacks = new Panel();

Literal litTrackback = new Literal();
litTrackback.Text = "<h1 class=\"trackbacks\">Pingbacks and trackbacks</h1><ol id=\"pingbacks\">";
pnlPingbacks.Controls.Add(litTrackback);

int pingbackCount = 0;
foreach (Comment comment in Post.Comments)
{
     CommentViewBase control = (CommentViewBase)LoadControl(pingbackPath);
     if (comment.Email == "pingback" || comment.Email == "trackback")
     {
          control.Comment = comment;
          control.Post = Post;
          pnlPingbacks.Controls.Add(control);
         
          pingbackCount++;
     }
}

As you can see in the code above you load a separate user control to show trackback. This control should be placed in your theme folder. Basically you can copy the code from CommentView.ascx. This is all you will need to place inside PingbackView.ascx:

<li id="id_<%=Comment.Id %>" class="pingback">
    <span class="pingbackDate">
        <%= Comment.DateCreated.ToString("dd MMM yyyy") %></span>
        at <span class="pingbackTime"><%= Comment.DateCreated.ToString("hh:mm") %></span>
    <span class="pingbackAuthor">
            <%= Comment.Website != null ? "<a href=\"" + Comment.Website + "\" class=\"url fn\">" + Comment.Content + "</a>" : "<span class=\"fn\">" +Comment.Author + "</span>" %>
    </span><%= AdminLinks %>
</li>

This is simple but dirty, I know. This is how I did it on my blog. I didn't have a time to create a clean code, but you can always extend and changeit. Would you do this in some other way?

With support from

Written by Janko in Tutorials, tagged under ,

Share:

Comments are closed