SaaS CMS has officially launched! Learn more now.

Nicklas Israelsson
Nov 15, 2010
  5096
(4 votes)

PageEntity for Community 4.0

PageEntities is a custom entity that links EPiServer Community functionality (rating, visits, comments etc) to ordinary EPiServer pages. It was first introduced by Per Hemmingson in this blog post and have since then been a popular addition for several projects. We use it on this website for instance.

The problem is that the code attached in his post is for Community 3.2 and doesn’t work together with the new Community 4 because of changes to the API. For details on the API changes, look for the tech note named:

Migrating EPiServer Community 3.2 to 4.0 and EPiServer Mail 4.4 to 5.0

Since last week we’re running Community 4 on world and in order to do the upgrade we needed to update the code for PageEntities. And since I’m such a friendly fellow I thought I’d share the changes made to the rest of the community. Hopefully I can save time for someone looking to do the same upgrade or adding PageEntities to a new community 4 project.

Changes made

The “big” change is that I removed the usage of SiteId in the code completely. Sites have been removed in favor of the category system in Community 4 and since we used the same SiteId for all our saved PageEntities we didn’t really need it. If you need it you need to make the appropriate changes to the code.

Most of the changes made were similar to this one:

From:

   1: DatabaseHandler.RunInTransaction(delegate
   2:  {
   3:      DatabaseHandler.GetScalar("spUpdatePageEntity",
   4:                                tpr.ID, (int) tpr.EntityType, tpr.SiteID);
   5:      UpdateEntity(tpr);
   6:  });

To:

   1: DatabaseHandler.Instance.RunInTransaction(delegate
   2:  {
   3:      DatabaseHandler.Instance.GetScalar("spUpdatePageEntity",
   4:                                tpr.ID, (int) tpr.EntityType);
   5:      UpdateEntity(tpr);
   6:  });

In other words, we started using the new static instances of the handlers. That was needed on several places but you get a lot of help from the compiler when doing this.

Another thing that was needed was a couple of methods to the PageEntityProvider class. This is because the interface IEntityProvider had gotten a few more methods. Namely: AddEntityInstance, RemoveEntityInstance, UpdateEntityInstance and GetSupportedOperations. They were all implemented similar to this (the difference is what method to use in the return statement):

   1: public IEntity AddEntityInstance(IEntity entity)
   2: {
   3:     if (!(entity is PageEntity))
   4:     {
   5:         throw new EPiServer.Common.Exceptions.NonCommittedEntityException(entity);
   6:     }
   7:     return PageEntityHandler.AddPageEntity((PageEntity)entity);
   8: }

All changes are available in  the attached .zip file at the bottom of this post.

SQL changes

Observe that the SQL script (PageEntityProviderSQL.sql) is changed so that all created stored procedures doesn’t use any SiteId any more.

Also note that I included a script to move data from one database to another in the same way that the migration script for community work. That script is called: PageEntity-MoveDataInTblPageEntityWithoutSiteId.sql

The script moves all data in your old database to the new excluding any values in the SiteId column.

There is no error handling in that script and it requires you to run the PageEntityProviderSQL script first to create the correct table in the new database. Remember to backup any database affected before attempting to run these scripts. The script don’t delete anything, but it’s better to be safe than sorry.

Files

Nov 15, 2010

Comments

Per Hemmingson
Per Hemmingson Nov 16, 2010 09:53 AM

Great job!

Mattias Vaglin Våglin
Mattias Vaglin Våglin Nov 19, 2010 03:12 PM

Great, this will be very useful.

A note, if you've removed the SiteID from the database, you should probably remove it from the nhibernate xml-mapping file as well, or queries won't work I think.

Nov 22, 2010 09:01 AM

Thanks Mattias!
You are correct regarding the nhibernate mapping. I had totally forgot about that. The zipfile has now been updated with a new version of the PageEntity.hbm.xml file where the siteID is removed.

Thanks again.

Please login to comment.
Latest blogs
A day in the life of an Optimizely Developer - London Meetup 2024

Hello and welcome to another instalment of A Day In The Life Of An Optimizely Developer. Last night (11th July 2024) I was excited to have attended...

Graham Carr | Jul 16, 2024

Creating Custom Actors for Optimizely Forms

Optimizely Forms is a powerful tool for creating web forms for various purposes such as registrations, job applications, surveys, etc. By default,...

Nahid | Jul 16, 2024

Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024