Web Developer, Software Engineer and Mixed Language Artist
RSS icon Email icon Home icon
  • EPiServer – Unpublished page custom properties

    Posted on March 25th, 2010 Jamie No comments

    One of the things I recently discovered about our EPiServer configuration is that quite often we have pages associated with our current page, that haven’t yet been published.

    An example of this is when we have a Press Release and other associated press releases that show up in a side bar.

    For live, this is fine because we don’t want unpublished items showing up in the list however in edit mode, our content team need to see that the items they’ve associated, work as expected.

    The error that we’ve encountered is in edit mode, for each associated press release, if the press release was unpublished we weren’t getting the custom properties for that page (in code), even though the rest of the page object was as expected.

    The reason for this is that EPiServer only seems to bring back the custom properties for pages that have been published.

    So how do we go about obtaining a full page object for pages which are yet to be published?

    This is how we did it:

    ?View Code CSHARP
    // If the page is not yet published.
    if (page.PendingPublish)
    {
        // Check if we're in edit mode
        SystemPageBase basePage = Page as SystemPageBase;
        if (basePage != null)
        {
             PageReference reference = new PageReference(pageId);
     
             // Get the individual Master Language Branch based on the reference above.
             PageVersion pageVersion =  PageVersion.List(reference)
                             .Where(m => m.IsMasterLanguageBranch).FirstOrDefault();
             if (pageVersion != null)
             {
                  page = DataFactory.Instance.GetPage(pageVersion.ID);
             }
        }
    }

    As a side-note, this also alerted us to the fact that page IDs in EPiServer are not always of type Int32. Usually when an Id comes back from EPiServer we can parse it as an int however the IDs that come back from page versions, actually include an underscore (i.e. they’re strings).

    Leave a reply