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 programmaticall开发者_如何转开发y redirect? Or is there a more elegant way to do it... Thanks in advance!
I don't know if there are a "Sitecore"-way of doing this, however I would try one of the following:
Instead of having a redirect I would just show the first child node of a specific template on the parent page.
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);
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; } }
}
}
精彩评论