65.9K
CodeProject 正在变化。 阅读更多。
Home

为 Community Server 2.0 提供的“最受欢迎的博文”控件

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (7投票s)

2006年7月5日

CPOL

4分钟阅读

viewsIcon

35796

downloadIcon

124

一个 Web 用户控件和类,用于显示单个博客中最受欢迎的博文。

引言

我为洛克希德·马丁公司的一个政府机构客户开发了一个博客 (http://www.pushingback.com),该客户使用 Community Server 2.0。该产品提供的众多优秀功能之一是博客聚合页面,该页面以单独的侧边栏小部件显示最受欢迎和评论最多的博文,并按降序排列。我们的一个项目要求是为我们正在做的单个博客实现此功能。由于 Community Server 2.0 在当时没有提供此功能,这意味着我必须自己构建它。

背景

Community Server 2.0 结合了 .NET 平台上许多以前单独的开源项目,这些项目提供了博客、论坛、照片库和其他功能。供应商 Telligent Systems 将其作为协作和知识管理平台进行营销。该产品可以进行相当大的自定义,因为几乎所有源代码都是开放的。洛克希德·马丁公司使用早期版本(Community Server 1.1)为该国 AMBER Alert 协调员构建了 一个外联网

特点

  • 可以通过标签属性设置显示的链接数量
  • 可以通过标签属性缩短标题长度

开发代码

自定义 Community Server 博客外观和感觉的最灵活方法是开发一个主题。在您的机器上安装 Community Server 2.0 后,[installdir]\Web\Themes\Blogs 文件夹 为您提供了九个示例主题供您选择。我需要为这个博客设计一个三列布局,所以我将“Marvin3”主题复制到一个新文件夹中。为了本文的目的,我们将该文件夹命名为“MyBlog”。

下一步是修改 LayoutTemplate.ascx,以便在博客上显示“最受欢迎”的小部件。修改后,右侧栏的 HTML 如下所示

<div id="sidebar-b">
  <Blog:WeblogCalendar runat="Server" id="Cal" /> 
  <Blog:SingleWeblogSearch runat="Server" id="sw" NAME="sw"/>
  <Blog:CategoryList ResourceTitleName = "Weblog_PostCategories_Label" 
               runat="Server" id="Categorylist1"/>
  <!-- Start of Most Viewed blog entries-->
  <MBC:MostViewed runat="server" id="mv" PageSize="3" />
  <!-- End of Most View blog entries-->
  <Blog:Subscriptions runat="Server" id="subs" />
</div>

正如您从此标签前缀中看到的那样

<%@ Register TagPrefix="MBC" 
    Namespace="MyBlogControls" Assembly="MyBlogControls" %>

我选择将控件的逻辑存储在一个单独的程序集中。这样做的主要原因是该项目所需的自定义控件数量。在绝大多数情况下,Community Server 2.0 的核心控件也是以这种方式构建的。

修改 LayoutTemplate.ascx 后,我需要为 MostViewed 标签创建一个皮肤来检索。按照约定,Community Server 中的皮肤文件名为“Skin-[skin name].ascx”,因此我创建了一个名为 Skin-MostViewed.ascx 的文件。这是整个文件

<%@ Register TagPrefix="CS" Namespace="CommunityServer.Controls" 
             Assembly="CommunityServer.Controls" %>
<%@ Control Language="C#" %>
<h3><CS:ResourceControl id="MostViewed" 
             runat="Server" ResourceName="MostViewed" /></h3>
<ul>
<asp:Repeater Runat="server" ID="posts">
 <ItemTemplate>
 <li>
  <asp:HyperLink Runat="server" id="TitleLink" />
 </li>
 </ItemTemplate>
</asp:Repeater>
</ul>

我将 Repeater 放在无序列表标签内,并将 HyperLink 控件放在列表项标签之间,以便样式表以与该列中其他控件相匹配的方式格式化它们。如果没有额外的标签,链接将显示为左对齐,而不是与右侧栏中的其他链接对齐。

完成 Skin-MostViewed.ascx 后,剩下的工作是检索最受欢迎的博文并将它们绑定到皮肤中的 Repeater 的逻辑。用于实际检索适当博文的大部分代码来自阅读 Community Server 的核心源代码和 Keyvan Nayyeri 的一些非常有用的帖子。这是实际获取适当帖子的代码

private WeblogPost[] GetMostViewedPosts(int PageCount)
{
    Weblog weblog = Weblogs.GetWeblog(CSContext.Current.ApplicationKey);
    BlogThreadQuery query = new BlogThreadQuery();
    query.SectionID = weblog.SectionID;
    query.SortBy = BlogThreadSortBy.MostViewed;
    query.SortOrder = SortOrder.Descending;
    query.PageSize = PageCount;
    query.PageIndex = 0;
    ThreadSet posts = WeblogPosts.GetBlogThreads(query);
    ArrayList al = new ArrayList(posts.Threads.Count);
    foreach(WeblogPost p in posts.Threads)
    {
        al.Add(p);
    }
    return (WeblogPost[])al.ToArray(typeof(WeblogPost));
}

此函数返回的数组是将绑定到 Skin-MostViewed.ascx 中的 Repeater 的数据源。当我测试我的代码时,我发现如果 query.SectionID 未设置为 weblog.SectionID,则显示的链接将指向整个 Community Server 安装,而不是选定的博客。PageCount 参数允许开发人员设置显示多少个“最受欢迎”帖子。我没有对一个值进行硬编码,而是创建了一个公共 PageSize 属性,可以使用标签属性将其设置为所需的值。

GetMostViewedPosts 必须在绑定数据之前调用。为了确保这一点,MostViewed 类重写了 AttachChildControlsDataBind。它还包含一个用于设置 Skin-MostViewed.ascxHyperLink 的特定属性的事件处理程序。自定义控件提供的一个有用的功能是能够设置控件中显示的博文标题的字符数。这是控制此行为的事件处理程序中的代码片段

if (TitleTextLength > 0) 
{
    titleLink.Text = Formatter.CheckStringLength(post.Subject, TitleTextLength);
    titleLink.NavigateUrl = BlogUrls.Instance().Post(post);
    titleLink.ToolTip = post.Subject;
}

结论

MostViewed 控件为 Community Server 2.0 中的各个博客提供了有用的增强功能。如果 Telligent 尚未将此功能构建到尚未推出的 Community Server 2.1 中,我可能会这样做,以便在未来的项目中更易于使用。

© . All rights reserved.