• Publisert
  • 1 min

Create your own Copy Page Event

I got into a case where I needed a copy page event, something EPiServer does not provide as a page event.

<p>I got into a case where I needed a copy page event, something EPiServer does not provide as a page event.</p>

<p>I was making a changelog functionality that read a text-property from all the existing versions of a page, and listed it out as an overview of all the changes made on the page. With this functionality the editor could update the property with some text when editing a new version to give the user a quick overview of the history of changes made to the page. (i.e "Version 1.3 of the document "Company Routines" added")</p><p>However there was a problem. When an editor copied a page to make a new one, the copied page got the last value from the original in the changelog-property because the new copied page got instantly published. So, I had to make some functionality that made sure that the property got cleared when a page of that type was copied. I also want to mention that this should not happen to the original page or a page that was only published, it should only happen to pages getting copied.</p>

First, I hooked up to the Page.Published-event in global.asax:

DataFactory.Instance.PublishedPage += CopyHelper.MakeSureEventlogIsEmptyWhenCopyingPage;
Then I implemented this method in the CopyHelper class:
Here the "ThePageType" is the page type that contains the property that I want to clear.
public static void MakeSureEventlogIsEmptyWhenCopyingPage(object sender, PageEventArgs e) 
{
  ThePageType currentPage = e.Page as ThePageType; 
  if (currentPage != null) 
   return; 
  if (e.PageLink != null) 
  {
   currentPage = DataFactory.Instance.GetPage(e.PageLink) as ThePageType;
   if (currentPage != null)
   {
    ThePageType clone = currentPage.CreateWritableClone() as ThePageType;
     if (clone != null)
     {
      clone.ChangeLog = string.Empty;
      DataFactory.Instance.Save(clone, SaveAction.CheckIn | SaveAction.ForceCurrentVersion);
     }
    }
   }
  return;
 }
Hope this simple example helps somebody.The only really special thing about this code is the check to se if currentPage is not null. The original page will not pass this test (it is not null), but for some reason the new version is in fact null and will pass it.

The only really special thing about this code is the check to se if currentPage is not null. The original page will not pass this test (it is not null), but for some reason the new version is in fact null and will pass it.

Hope this simple example helps somebody.