More in this section

Forums / Developing with Sitefinity / Custom Dashboard Module to Show Recent Comments

Custom Dashboard Module to Show Recent Comments

3 posts, 1 answered
  1. Jay
    Jay avatar
    51 posts
    Registered:
    16 Jul 2008
    21 May 2010
    Link to this post
    I'm using the Blogs module in Sitefinity to build a blog for a client site and everything works great.  The client wants moderated comments which also is no problem except that someone has to keep logging in and checking for new comments which can be tedious if there are multiple blogs. 

    I've added email notification for comments based on a nice post that Gabe Sumner wrote a while back... but I'm also exploring the idea of writing a "Recent Comments" modules that could be added to the main Sitefinity dashboard.

    I've done some digging through the API docs and through the forums and have come up with the following code (mostly based on something Ivan posted in another thread) but I'm afraid I'm now a little over my head. 

    Has anyone created something like this before?  Or would anyone from Telerik be able to provide the code for one of the existing dashboard modules so that I could use it as a baseline?  I'm happy to share the module in the marketplace once it's completed.  I could just use a little nudge in the right direction so that I don't spend a lot of hours in the wrong direction.  :)


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik;
    using Telerik.Personalization;
    using System.Web.UI.WebControls;
    using System.Web.UI;
    using System.ComponentModel;
    using System.Diagnostics;
     
    /// <summary>
    /// A dashboard module to display recent comments
    /// </summary>
    public class RecentCommentsModule : WebModule, IPersonalizedDashboard
    {
        public override string Name
        {
            get { return "RecentCommentsModule"; }
        }
     
        public override string Title
        {
            get { return "Recent Comments Module"; }
        }
     
        public override string Description
        {
            get { return "Recent Comments Module"; }
        }
     
        public override IList<Telerik.Web.IToolboxItem> Controls
        {
            get { return null; }
        }
     
        #region IPersonalizedDashboard Members
     
        public IDictionary<string, string> DashboardSettings
        {
            get
            {
                IDictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("recentComments", "Recent Comments");
                return dict;
            }
        }
     
        public IList<IPersonalizedDashboardItem> DashboardItems
        {
            get
            {
                List<IPersonalizedDashboardItem> list = new List<IPersonalizedDashboardItem>();
                if (PersonalizationManager.DefaultInstance.GetGlobalValue("recentComments", Telerik.Personalization.Configuration.ConfigHelper.Handler.DashboardVisibilitySettings.IsVisible("recentComments")))
                {
                    list.Add(new RecentCommentsDashboardItem());
                }
                return list;
            }
        }
     
        #endregion
     
        public class RecentCommentsDashboardItem : CompositeControl, IPersonalizedDashboardItem
        {
            protected override void CreateChildControls()
            {
                // Not sure what to put here.  Programmatically add the GridView?
            }
      
            #region IPersonalizedDashboardItem Members
     
            public virtual object DataSource
            {
                get {
                    Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager();
                    System.Collections.IList listOfComments = contentManager.GetComments("DateCreated DESC", "");
                    return listOfComments;
                }
                set {
                }
            }
             
            [Category("Appearance")]
            public virtual string Title
            {
                get
                {
                    return "Recent Blog Comments";
                }
                set
                {
                }
            }
     
            [Category("Behavior")]
            public virtual string CloseSettingKey
            {
                get
                {
                    return string.Empty;
                }
                set
                {
                }
            }
     
            [Category("Behavior")]
            public virtual string RowPerPageSettingKey
            {
                get
                {
                    return string.Empty;
                }
                set
                {
                }
            }
     
            public virtual string TemplatePath
            {
                get
                {
                    return string.Empty;
                }
                set
                {
                }
            }
     
            [DefaultValue(true)]
            public virtual bool ShowProviderName
            {
                get
                {
                    return true;
                }
                set
                {
                }
            }
     
            /// <summary>
            /// Gets or sets the index of the provider column.
            /// </summary>
            /// <value>The index of the provider column.</value>
            [DefaultValue(-1)]
            public virtual int ProviderColumnIndex
            {
                get
                {
                    return -1;
                }
                set
                {
                }
            }
     
            /// <summary>
            /// Gets or sets the name of the module.
            /// </summary>
            /// <value>The name of the module.</value>
            [Browsable(false)]
            public string ModuleName
            {
                get
                {
                    return string.Empty;
                }
                set
                {
                }
            }
     
            #endregion
        }
    }
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    31 May 2010
    Link to this post
    Hi Jay,

    You have to implement IPersonalizedDashboard, IPersonalizationRecentItemProvider interfaces. It is not necessary to create a new control - in your case RecentCommentsDashboardItem because this means that you need to create the Grid manually inside CreateChildControls. You can use the built-in PersonalizedDashboardItem . You have to create a helper class RecentComments where you map the Text, Author and other IContent properties. Note that the IComment does not have property that specifies the DataCreated, so you cannot extract it directly.

    In GetRecentItems you have to make the relation IContent - RecentComments - PersonalizationRecentItem

    Inside DashboardItems you have to create an instance of PersonalizedDashboardItem and set its properties. You have to set the TemplatePath of the control. You can create a custom template inside Sitefinity\Admin\ControlTemplates\Personalization\. The template should contain declaration of RadGrid control and BoundFields that exist in your data source

    sample code

    <%@ Control Language="C#" AutoEventWireup="true" %>
     
    <table class="lastModifiedModules">
        <caption>
            <asp:Label ID="Title" runat="server"></asp:Label>
            <a title="Hide this panel"><asp:Image ID="closeImage" runat="server" ImageUrl="~/Sitefinity/Admin/Themes/Default/Images/dialogCloseLight.gif" /></a>
        </caption>
        <tfoot>
            <tr>
                <td>  
                    <a id="A1" href="~/Sitefinity/Admin/Modules.aspx?module=Blogs" runat="server"><asp:Literal ID="Literal1" runat="server" Text="AllBlogs" /></a>
                    <asp:Label ID="RowPerPageLabel" runat="server" AssociatedControlID="RowPerPageDropDownList" Text="RowPerPageText"></asp:Label>
                    <asp:DropDownList ID="RowPerPageDropDownList" AutoPostBack="true" runat="server"></asp:DropDownList>
                              
                </td>
            </tr>
        </tfoot>
           <tbody>
            <tr>
                <td>
                    <asp:UpdatePanel ID="AjaxPanel" runat="server" UpdateMode="Conditional">
                        <ContentTemplate>
                            <asp:GridView ID="Grid" runat="server" GridLines="None" AutoGenerateColumns="false">
                                <Columns>
                                    <asp:BoundField DataField="Title" HeaderText="Text" />
                                    
                                </Columns>
                                <EmptyDataTemplate>
                                    <asp:Literal ID="NoRecordLiteral" runat="server" Text="NoRecordLiteral" />
                                </EmptyDataTemplate>
                            </asp:GridView>
                 
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </td>
            </tr>
        </tbody>
    </table>


    #region IPersonalizedDashboard Members
     
       public IDictionary<string, string> DashboardSettings
       {
           get
           {
               IDictionary<string, string> dict = new Dictionary<string, string>();
               dict.Add("recentComments", "Recent Comments");
               return dict;
           }
       }
     
       public IList<IPersonalizedDashboardItem> DashboardItems
       {
           get
           {
     
     
               IList<IPersonalizedDashboardItem> list = new List<IPersonalizedDashboardItem>();
               PersonalizedDashboardItem control = new PersonalizedDashboardItem();
     
               control.ModuleName = "BlogEditorModule";
               control.Title = "ShowLastComments";
               control.CloseSettingKey = "closeSettingKey";
               control.RowPerPageSettingKey = "5";
               control.TemplatePath = "~/Sitefinity/Admin/ControlTemplates/Personalization/RecentBlogComments.ascx";
     
               int rowPerPage = PersonalizationManager.DefaultInstance.GetModuleValue("closeSettingKey", "BlogEditorModule", ModuleSettingConstants.DefaultRecentItem);
     
               control.DataSource = GetRecentItems(rowPerPage -1);
               list.Add(control);
     
     
                 control.Saved += delegate(object sender, PersonalizedDashboardItemSettingSaveEventArgs e)
                               {
                                   ((PersonalizedDashboardItem)sender).DataSource = GetRecentItems(rowPerPage -1);
                               };
                 return list;
           }
     
       }
     
     
       #endregion
     
       
       #region IPersonalizationRecentItemProvider Members
     
       public IList<PersonalizationRecentItem> GetRecentItems(string userName, int count)
       {
           IList<PersonalizationRecentItem> items = new List<PersonalizationRecentItem>();
           List<RecentComments> com = GetRecentComments(-1);
     
           if ((com != null) && (com.Count > 0))
           {
               for (int i = 0; i < com.Count; i++)
               {
                   PersonalizationRecentItem item = new PersonalizationRecentItem();
     
                   item.Type = "Comment Item";
                   item.Title = com[i].Text;
                   items.Add(item);
                   if (i == (count - 1))
                   {
                       break;
                   }
               }
           }
     
           return items;
       }
     
     
       public List<RecentComments> GetRecentComments(int rowPerPage)
       {
           List<RecentComments> list = new List<RecentComments>();
           Telerik.Cms.Engine.ContentManager contentManager = new Telerik.Cms.Engine.ContentManager("Blogs");
           IList comments = contentManager.GetComments("DateCreated DESC", string.Empty);
           foreach (IComment c in comments)
           {
               RecentComments rc = new RecentComments(c.Text, c.Author);
               list.Add(rc);
           }
           return list;
       }
     
       public IList<PersonalizationRecentItem> GetRecentItems(int count)
       {
           return GetRecentItems(HttpContext.Current.User.Identity.Name, count);
       }
     
       #endregion
     
       public class RecentComments
       {
            
           private readonly string _text;
           private readonly string _author;
     
           public string Text
           {
               get { return _text; }
           }
     
           public string Author
           {
               get { return _author; }
           }
     
           public RecentComments(string text, string author)
           {
               _text = text;
               _author = author;
           }
       }

    Most of the methods in PersonalizationManager are not created for using in a custom implementations and extending the Dashboard is hard task.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
    Answered
  3. Jay
    Jay avatar
    51 posts
    Registered:
    16 Jul 2008
    07 Jun 2010
    Link to this post
    As always, your support is fantastic.  This was exactly what I needed.

    Thanks a million!
Register for webinar
3 posts, 1 answered