We've got a sitemap generator class that inherits IHttpHandler. Because this is in a shared project with multiple sites, I want to ensure that when sitemap.xml is called, the correct tree structure is drawn out based on the site I'm visiting. I figure hooking into the Sitecore pipeline is the key, but can someone guide me to the proper one that I'd replace the standard .NET one with? Thanks much.
1 Answer
To answer your question directly; you would need to get in after the SiteResolver. Since you need to know what site you're on, to deliver your sitemap. It sits near the top in the <httpRequestBegin>
pipeline.
<httpRequestBegin>
<processor type="Sitecore.Pipelines.PreprocessRequest.CheckIgnoreFlag, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.HttpRequest.EnsureServerUrl, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.HttpRequest.StartMeasurements, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.HttpRequest.IgnoreList, Sitecore.Kernel" />
<processor type="Sitecore.Pipelines.HttpRequest.SiteResolver, Sitecore.Kernel" />
So you would not be implementing an IHttpHandler as you suggest, you would instead inherit from Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
.
If I may; I'd like to add a few architectural considerations to the mix as well.
Be careful not to "render" your sitemap in real time on these requests. It's usually a very "heavy" operation and you open yourself up quite easily to a denial-of-service attack by doing so.
Instead I propose you add sitemap generation as a scheduled task in Sitecore, to regularly (like hourly/daily/as appropriate) generate sitemaps for each site, and the processor in question only concerns itself with streaming back that generated sitemap to the client.
IHttpHandlers
usually execute before a Sitecore.Context
is established - this is exactly the job of the <httpRequestBegin>
pipeline.
EDIT: Added based on comments
Creating the sitemaps in the context of a Scheduled Task, you can indeed implement any class you like. Sitecore, however, expects to find the following method signature on whatever class you implement.
public void Execute(Item[] items, Sitecore.Tasks.CommandItem command, Sitecore.Tasks.ScheduleItem schedule)
A good guide for these can be found here: How To Create A Sitecore Scheduled Task
-
Thanks, Mark, this gives me the direction to go, and good thought on the architectural concerns. So for the scheduled task, I could make that any class I want that can then access the context of the site and go from there. Commented Oct 26, 2016 at 16:13
-
Yea. I'll edit my answer to explain this in a bit more detail.– Mark Cassidy ♦Commented Oct 26, 2016 at 16:24