Patrick Ibert
Aug 18, 2011
  5103
(4 votes)

Implement a comment system using EPiServers datastore by using Paul Smith's DynamicDataStoreSession class

Hi!

First I would want to start this post by thanking Paul Smith for taking the time to point out the mistakes I had done in the last post I did about this, I learned a lot how to use the datastore in a correct way from his help! Thanks Paul!

This post is about how we can use Paul Smith's DynamicDataStoreSession class with very little code to implement a comment system for our EPi pages using the EPiServer datastore.

The idea is that I will create an Usercontrol that I can place on a page where I want people to be able to comment the content.


I start off with creating my entity class:


     public class Comment
    {
        public Identity Id { get; set; }
        [EPiServerDataIndex]
        public Guid PageGuid { get; set; } 
        public string UserName { get; set; }
        public string Location { get; set; }
        public string UserComment { get; set; }
        public DateTime Date { get; set; }
    }


Note that my PageGuid property has the attribute [EPiServerDataIndex], it will tell the DDS to map the property to an indexed database column, which will speed up retrieving the data.

I then make a factory class were I will add my datastore methods:


     public static IEnumerable<Comment> GetComments(Guid pageGuid)
        {
            var session = new DynamicDataStoreSession<Comment>();
            return session.Find().Where(p => p.PageGuid).Equals(pageGuid).Go().OrderByDescending(p => p.Date);
        }

        public static void SubmitComment(string comment, Guid pageGuid)
        {
            var store = new DynamicDataStoreSession<Comment>();
            var newComment = new Comment
            {
                Id = Identity.NewIdentity(Guid.NewGuid()),
                UserName = Membership.GetUser().UserName,
                Location = "Stockholm, Sweden",
                UserComment = comment,
                Date = DateTime.Now,
                PageGuid = pageGuid
            };
            store.Save(newComment); 
           

        }


My usercontrol looks like this:

<asp:Repeater ID="repComments" runat="server">
                <ItemTemplate>
                   <tr>
                   <td><%# DataBinder.Eval(Container.DataItem, "UserName")%> (<%# DataBinder.Eval(Container.DataItem, "Location")%>) </td>
                   <td><br /><%# DataBinder.Eval(Container.DataItem, "UserComment")%></td><br /><br />
                 </tr> 
                </ItemTemplate>
                </asp:Repeater>


and in the code behind:

  protected void Page_Load(object sender, EventArgs e)
        {
           repComments.DataSource = ArticleComment.GetComments(CurrentPage.PageGuid);
           repComments.DataBind();
       }

        protected void btnComment_Click(object sender, EventArgs e)
        {
            if (tbComment.Text == "")
                return;
            ArticleComment.SubmitComment(tbComment.Text, CurrentPage.PageGuid);
            repComments.DataSource = ArticleComment.GetComments(CurrentPage.PageGuid);
            repComments.DataBind();
            tbComment.Text = "";
        }


I hope you had fun and found this post useful :)

Aug 18, 2011

Comments

Aug 18, 2011 03:54 PM

Hi Patrick, nice blog post. I learned some stuff! :)

But doesn't the code below mean that the [EPIServerDataIndex] is an attribute to UserName, not PageGuid?

public Guid PageGuid { get; set; }
[EPiServerDataIndex]
public string UserName { get; set; }

Maybe it's my c# skills that isn't up to par when it comes to attributes.

smithsson68@gmail.com
smithsson68@gmail.com Aug 18, 2011 04:12 PM

Hi Patrick!

Your code is looking great now except for the small mistake that Toni has pointed out. You need to declare the attribute on top on the member. When you change this you will find that your app crashes as the class definition no longer matches the DDS store definition. You get around this easily by adding the following attribute to your Comment class:

[EPiServerDataStore(AutomaticallyRemapStore=true)]

With this added your store will automatically align to the class the first time the store is accessed.

Thanks for sharing!

/Paul

Patrick Ibert
Patrick Ibert Aug 18, 2011 05:03 PM

Hi!
Oh Yea I thought about that before if it wasent wrong, I guess I missunderstood that part some.
Thanks for pointing it out, updated the post with it.

/Patrick

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024