65.9K
CodeProject is changing. Read more.
Home

N2CMS Templates RSS Feed Fix

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Jun 14, 2010

CPOL
viewsIcon

9191

N2CMS Templates RSS Feed Fix

N2CMS comes with some pretty fully featured examples, the most useful to me is the N2.Templates example site. Unfortunately, as it stands, the RSS feed is broken and gives the error:

Cannot use filters when using MaxResults, sorry.

Fortunately, a fix can be found in the N2.Templates.Mvc project. You just need to replace the following functions in N2.Templates.Items.RssFeed:

public virtual IEnumerable<ISyndicatable> GetItems()
{
  foreach (ISyndicatable item in N2.Find.Items
      .Where.Detail(SyndicatableDefinitionAppender.SyndicatableDetailName).Eq(true)
      .Filters(GetFilters())
      .MaxResults(NumberOfItems)
      .OrderBy.Published.Desc
      .Select())
  {
      yield return item;
  }
}

private ItemFilter[] GetFilters()
{
  ItemFilter[] filters;
  if (FeedRoot != null)
      filters = new ItemFilter[] { new TypeFilter(typeof(ISyndicatable)), 
		new AccessFilter(), new ParentFilter(FeedRoot) };
  else
      filters = new ItemFilter[] { new TypeFilter(typeof(ISyndicatable)), 
		new AccessFilter() };
  return filters;
}

with:

public virtual IEnumerable<ISyndicatable> GetItems()
{
  var filter = new AccessFilter();
  var q = N2.Find.Items.Where.Detail
	(SyndicatableDefinitionAppender.SyndicatableDetailName).Eq(true);
  if (FeedRoot != null)
      q = q.And.AncestralTrail.Like(Utility.GetTrail(FeedRoot) + "%");
  foreach (ContentItem item in q
          .OrderBy.Published.Desc
          .Select().Take(NumberOfItems))
  {
      var syndicatable = item as ISyndicatable;
      if (syndicatable != null && filter.Match(item))
      {
          yield return syndicatable;
      }
  }

And add ‘using System.Linq’ to the top of the file.

Easy… though it would be nice to see this fix, make it into the trunk repository!