Creating a Rss sample for ETF using NuGet

This is a follow-up post of sorts to Creating a NuGet-package for ETF so if you haven’t read that go ahead and do so. I’ll wait.

Ted posted on creating a rss feed using EPiServer and ETF so in this post I’ll show how that sample could be distributed using NuGet. While the most common usage for NuGet probably is distributing assemblies (and their dependencies) another scenario that’s also possible is small usage samples / code files. To give another example than creating the rss template for ETF could perhaps be a package that contains the PTB tab classes for the standard EPiServer tabs or a base for creating integration tests against EPiServer. This will probably be even more viable when we can automatically push new packages to the EPiServer feed instead of mailing them.

The manifest file

This package will depend upon ETF but other than that it’s pretty straight forward

<?xml version="1.0" encoding="utf-8"?> 
<package> 
  <metadata> 
    <id>ETF.RssSample</id> 
    <version>1.0</version> 
    <authors>Ted Nyberg</authors>
    <dependencies>
      <dependency id="ETF" version="1.1" />
    </dependencies>
    <description>A sample package for using RSS with ETF.</description> 
    <language>en-US</language>
    <licenseUrl>http://etf.codeplex.com/license</licenseUrl>
    <projectUrl>http://etf.codeplex.com/</projectUrl>
  </metadata> 
</package>

Adding files to a NuGet package

If you take a look at Teds post to get a RSS going we need to add a template (.aspx) and a PageTypeBuilder class that represents the rss feed. We can simple add these files to the content folder like this

Etf.Nuget.RssSample (folder)
  ETF.RssSample.nuspec
  content (folder)
    Pages (folder)
      RssPageTemplate.aspx.pp
      RssPageTemplate.aspx.cs.pp
      RssPageTemplate.aspx.designer.cs.pp
    PageTypes (folder)
      RssPage.cs.pp

Source-file transformation

You’ll probably notice that all the code files above have a “.pp” file ending. This is required so that NuGet performs source-file transformation upon installation. In our case we want to use the default namespace in the project we’re installing the rss sample into. So let’s say I have a project with the root namespace Stefans.Awesome.PageTypes. When I add the RssPage it will then have the namespace Stefans.Awesome.Project.Pages. If we take a look at the RssPage.cs.pp it has the following content

using System;
using EPiServer;
using EPiServer.Core;
using EPiServer.Filters;
using PageTypeBuilder;
using TemplateFoundation.PageTypes;

namespace $rootnamespace$.PageTypes
{
    [PageType(
        Name = "RSS Feed",
        Description = "Used to publish an RSS feed for a section of the site",
        Filename = "/Templates/Pages/RssPageTemplate.aspx")]
    public class RssPage : RssPageBase
    {
        [PageTypeProperty(
            EditCaption = "Root page",
            HelpText = "All pages beneath this page will be included in the RSS feed",
            Required = true)]
        public virtual PageReference RssPageLink { get; set; }

        /// <summary>
        /// Returns all pages to include in the RSS feed
        /// </summary>
        public override PageDataCollection GetPages()
        {
            if (PageReference.IsNullOrEmpty(RssPageLink))
            {
                throw new ArgumentNullException("RssPageLink", "No page link specified");
            }

            var pages = DataFactory.Instance.GetChildren(RssPageLink);

            return FilterForVisitor.Filter(pages);
        }
    }
}

This is an ordinary c-sharp class except for line 8. The $rootnamespace$ is part of the transformation and instructs the engine to replace that with the default namespace of the project we’re installing the project into. While this is the most common transformation variable there are others available too, a list can be found on msdn. The other files in the package contains the same $rootnamespace$ variable too.

Installation

When we add this package to our project it will go fetch ETF (and it’s dependencies) and then add all the code files from content and perform the source file transformation. So while browsing a forum post and copy/pasting code isn’t all that time consuming it won’t be as quick and / or smooth as this.