Anders G. Nordby

Senior Systems Consultant at CGI

Monthly Archives: June 2012

Distinguishing between newly created and updated pages

In my current project, I’m building a news feed, listing events like “A new article was created: [ArticleName]” and “The article [ArticleName] was updated”. Thus, I had the need for distinguishing between a newly created page and a page update. For some reason, I initially expected there to be some kind of property I could check against, and spent quite some time trying to find it, finally realizing that it was not there. Then, I tried comparing various dates, as the published date, the created date, etc.

Finally I got this very simple idea: keep a counter for how many times the page is published. If it’s the first time, the page is new – otherwise it’s an update.

The site uses PageTypeBuilder, and I have a BasePage (deriving from TypedPageData).
So, in my BasePage, I created the following:

[PageTypeProperty(
    EditCaption = "PublishCount",
    DisplayInEditMode = false,
    DefaultValue = 0,
    Tab = typeof(Tabs.Advanced),
    Type = typeof (PropertyNumber)
    )]
public virtual int? PublishCount { get; set; }

public bool IsNew
{
    get { return !PublishCount.HasValue || PublishCount.Value == 0; }
}

Then, in my Global.asax.cs, I created the following handler:

private void PublishCounter(object sender, PageEventArgs e)
{
    var basePage = e.Page as Model.BasePage;
    if (basePage == null)
        return;

    if (basePage.PublishCount == null)
        basePage.PublishCount = 0;
    else
        basePage.PublishCount++;
}

…and then of course handling it in the Application_Start method:

DataFactory.Instance.PublishingPage += PublishCounter;

Now the IsNew method tells me what I need (with the added bonus of being able to know the PublishCount, should I ever need it).