Blog posts about episerver

  • Randomize a page data collection in EPiServer

    August 24, 2011 | Tags: EPiServer, ASP.NET, C#, Snippet
    Here's a neat little function that will randomize the order of a PageDataCollection. The random order will be changed on each and every call (using a different random seed based on current date and time in ticks). If you're using the result in a paged list you might want to consider tweaking the code somewhat, either caching the list or by caching the seed and send it into the function instead of using the DateTime.Now.Ticks on every call.
    public static PageDataCollection Shuffle(PageDataCollection originalCollection) {
          
    // Create randomizer
    Random rnd = new Random((int)DateTime.Now.Ticks);

    // Create an array of PageData from the PageDataCollection sent to this function
    PageData[] pages = new PageData[originalCollection.Count];
    originalCollection.CopyTo(pages, 0);

    // Create a byte array of the same length and fill with random values
    byte[] randomBytes = new byte[pages.Length];
    rnd.NextBytes(randomBytes);

    // Sort array of PageData based on random values in the byte array
    Array.Sort(randomBytes, pages);

    // Return the randomly sorted PageDataCollection
    return new PageDataCollection(pages);
    }

  • Get only published pages

    March 24, 2011 | Tags: EPiServer, Troubleshooting, Snippet
    Just getting the children for a page in EPiServer CMS 5/6 will return all pages, even unpublished ones. Since you might not want this behaviour there's a way to filter out all the published (or unpublished - if that's your cup of tea) using FilterPublished:

    using EPiServer.Filters;

    ....

    PageDataCollection myPages = GetChildren(myParentPage);
    new FilterPublished(PagePublishedStatus.Published).Filter(myPages);

  • Get LinkURL in page´s language and not current

    March 21, 2011 | Tags: EPiServer, Troubleshooting, Snippet
    This is a kind of weird thing about the LinkURL property of a PageData object in EPiServer. Even though the page in in a specific language, the LinkURL will ignore that and return a link to the current language version instead.

    If you want the URL to be in the same language as the page is, instead of:
    string url = page.LinkURL;
    ..start using AddLanguageSelection and pass the page´s language ID:
    string url = EPiServer.UriSupport.AddLanguageSelection(page.LinkURL, page.LanguageID);

  • Time controlled publishing in EPiServer

    February 24, 2011 | Tags: EPiServer, Troubleshooting
    There's a neat way in EPiServer to time control publishing of pages. It's really easy when it comes to new pages, but when you want a new version of an existing page it turns out to be a little bit more rocket surgery.

    Here's how to do it:
    • Edit the page with your changes. Leave publish dates as is, you will set your date elsewhere.
    • Select "Save and view" (NOT PUBLISH!)
    • Two buttons are displayed, click "Ready for publish".
    • Goto the version list tab.
    • Last in the listing where your new version is there's a time schedule icons to the right. Click on it.
    • Tick the activate checkbox and enter time and date as you please.
    • Save and your new version of the page is now awaiting automatic publishing.

    Please note, that if you do another version of your page you'll need to go through these steps again.

  • Prevent EPiServer UrlRewrite

    November 09, 2010 | Tags: EPiServer, ASP.NET, SEO, Troubleshooting
    Sometimes it's needed to turn off EPiServers function for Url rewrite, like if you for instance writes a link-element with a canonical url (which helps search engine optimization (SEO)). The UrlRewriteProvider works as a HTTP-module, which means that it's running after your page is rendered, parsing all your Urls in a predefined list of elements and attributes.

    To prevent EPiServer's Url rewrite to occur do the following to your element:
    1) Add an id-attribute to your element
    2) Add a special EPiServer attribute to prevent the rewrite


    Original code:
    <link rel="canonical" href="the/url/not/to/rewrite" />

    Updated code:
    <link rel="canonical" id="canonical1" <%=EPiServer.Web.UrlRewriteProvider.UrlPreventRewriteAttribute %>="true" href="the/url/not/to/rewrite" />