Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Saving a page programmatically without versioning



Why would I want to save a page without creating a version? Well...

I have a listing of pages, similar to news. All the news pages are subpages to the Archive page. In the CMS Edit view, I want all the news pages to be ordered by a custom date "myDate". My approach for this was to set the Archive page to order it's subpages according to their sort index. Then I would hook up on the Page Published event for news pages. Whenever that runs I would fetch all the News pages, sort them by myDate and then set the sort order accordingly. Here's what I didn't anticipate: Whenever I change the sort order by code and save the page, the publish event is triggered again, creating an infinite loop. Hence my question, can I modify the sort index without affecting anything else?

Here's how I modify the sort index: 

public void PublishedPage(object sender, PageEventArgs e)
            PageReference consertSidan = new PageReference(7);
            List children = DataFactory.Instance.GetChildren(consertSidan).Cast().ToList();

            children.Sort((emp1, emp2) => emp1.EventStartTime.CompareTo(emp2.EventStartTime));

            for (int i = 0; i < children.Count;i++ )
                ConsertPage page = children[i];
                PageData page2 = page.CreateWritableClone();
                page2.Property["PagePeerOrder"].Value = 100 + (i * 10);
                EPiServer.DataFactory.Instance.Save(page2, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

If there's another way to sort pages in the cms edit view according to a custom date field, I'd be glad to hear it. Thanks.

Sep 17, 2015 16:42

"Whenever I change the sort order by code and save the page, the publish event is triggered again, creating an infinite loop".

Solution 1: Just hook into the publishing event instead of published to set the property of the current page but dont call the save method.

Solution 2: The publish event will always triger whenever you publish the page either programmaticaly or in GUI. The only thing you can do is to create a bool property on the page something like "IsAlreadySorted" and on first publish event, set this up and any other subsequent events use this as a flag. To do this, you have to hook into publishing and published event both.

Edited, Sep 17, 2015 18:04

You can't obviously save the page in an event. But what you could do, is to subscribe to the PublishingContent (or PublishingPage) event instead. This event occurs before the page is persisted to the database, then there is no need to call the save method again. Why are you updating all children? Isn't it enough to just update the page that was published? Maybe I missunderstood something.

Here's an example on how you could do it (untested of course):

public class InitializationModule : IInitializableModule
    public void Initialize(InitializationEngine context)
        var events = ServiceLocator.Current.GetInstance<IContentEvents>();

        events.PublishingContent += this.PublishingConcert;

    public void Uninitialize(InitializationEngine context)
        var events = ServiceLocator.Current.GetInstance<IContentEvents>();

        events.PublishingContent -= this.PublishingConcert;

    private void PublishingConcert(object sender, ContentEventArgs e)
        var concertPage = e.Content as ConcertPage;

        if (concertPage == null)

        concertPage[MetaDataProperties.PagePeerOrder] = int.Parse(concertPage.EventStartTime.ToString("yyyyMMddHHmmss"));

Just convert the date to an integer.

Sep 17, 2015 18:06

Oh forgot, if you want to change the sort order you could just do:

int.Max - int.Parse(concertPage.EventStartTime.ToString("yyyyMMddHHmmss"))
Sep 17, 2015 19:27

Johan Petersson: I wanted to update all children so the sort order would be correct for all children. Instead of setting the sort order to myDate, I was oddly enough planning on setting the sort order to the list index of each page after sorting them. So page A = 1, B = 2 etc. Of course, using the date in integer form is much better. That way I only have to update the one page that's getting published. Thanks a lot!

Sep 18, 2015 8:46
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.