views:

599

answers:

2

I have this Newsletter page, that has the actual newsletters nested underneath it for each month of the year.

I want to be able to have the parent page redirect to the latest published page. What would be the best way to do that?

I don't really have any idea of where to start with this one... Should I make a sub layout user control and programmatically redirect? Or is there a more elegant way to do it... Thanks in advance!

+3  A: 

I don't know if there are a "Sitecore"-way of doing this, however I would try one of the following:

  1. Instead of having a redirect I would just show the first child node of a specific template on the parent page.

  2. As you suggested, make a sublayout that redirects the user to the Newsletter.

Here is a code that should work for you. It gets the latest published newsletter you could use it in either solution above.

Item[] childs = currentItem.Axes.GetDescendants();

var itm = childs.AsQueryable().Where(c => c.TemplateName.ToLower().
Contains("newsletter")).OrderByDescending(c => c.Publishing.PublishDate).Take(1);
Zooking
Thanks for the suggestion Zooking. Where exactly should I put this code? In the code-behind of a sub-layout, instead of a redirect? Thanks.
SkippyFire
In either case you should put the code in the code-behind of the sublayout that is used in the "first"-page. Then you can choose to either redirect the user to the item returned by the code or just show the item directly in the sublayout. I am no search engine guru but I think that you will get better ranking in the search engines if you do the redirect, that way a specific newsletter has only one URL.
Zooking
A: 

Set the layout of the main page (i.e. the one you want to redirect from) to be an ashx generic handler. That would mean there is not UI code involved in any way.

You can do it with a lot less code, as long as the following assumptions are true: 1. All child items are newsletters 2. The latest is at the top (e.g. items are sorted by release date)

namespace YourSite
{
 public class NewsletterHandler : IHttpHandler
 {
  public void ProcessRequest(HttpContext context)
  {
   Item item = Sitecore.Context.Item.Children[0];
   string url = Sitecore.Links.LinkManager.GetItemUrl(item);
   context.Response.Redirect(url);
  }

  public bool IsReusable { get { return false; } }
 }
}

If the assumptions above are wrong, finding the latest published may not be as simple as getting the publish date as described by Zooking. It may be, but if you make a fix to an older newsletter and re-publish, that code may fail (i think?). If it's fine, no problem, Zooking is correct, otherwise you may want to actual give each newsletter a specific date field .. in that case, use the code here in your handler.

namespace YourSite
{
 public class NewsletterHandler : IHttpHandler
 {
  public void ProcessRequest(HttpContext context)
  {
   Item[] children = Sitecore.Context.Item.Axes.GetDescendants();
   Item item = children.AsQueryable()
      .Where(c => c.TemplateName.ToLower()
      .Contains("newsletter"))
      .OrderByDescending(c => DateTime.Parse(c["PublishDate"])).FirstOrDefault();
   string url = Sitecore.Links.LinkManager.GetItemUrl(item);
   context.Response.Redirect(url);
  }

  public bool IsReusable { get { return false; } }
 }
}
misteraidan