In Kentico 13, you can retrieve pages using DocumentHelper.GetDocuments and then generate a standard RSS feed based on the retrieved pages.
The following code snippet retrieves documents using DocumentHelper.GetDocuments(), filters them based on your criteria (site, path, culture, etc.), and then creates an RSS feed by adding items for each page retrieved.
Make sure to replace placeholders like YourSiteName, adjust path filters, and customize properties according to your Kentico instance and desired RSS feed information.
Using XmlDocument, XmlElement, and related methods can provide a structured and cleaner way to generate XML for the RSS feed in C#.
Here’s an example demonstrating how to create an RSS feed in Kentico 13 using XmlDocument, XmlElement, and related methods:
using CMS.DocumentEngine; using CMS.Helpers; using System; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Xml; // Your method to generate RSS feed XML public ActionResult RSSNews() { // Get documents using DocumentHelper var pages = DocumentHelper.GetDocuments() .Path("/%", PathTypeEnum.Children) // Adjust the path filter as needed .OnSite("YourSiteName") // Replace 'YourSiteName' with your actual site name .Culture("en-US") // Replace with the desired culture .Published() .MenuItems() .OrderByAscending("NodeLevel") .TopN(20) // Replace with the desired number of pages to retrieve .TypedResult; // Create XmlDocument to build the RSS feed var xmlDoc = new XmlDocument(); var rssNode = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); xmlDoc.AppendChild(rssNode); // Create the root <rss> element var rssElement = xmlDoc.CreateElement("rss"); rssElement.SetAttribute("version", "2.0"); xmlDoc.AppendChild(rssElement); // Create the <channel> element var channelElement = xmlDoc.CreateElement("channel"); rssElement.AppendChild(channelElement); // Add metadata for the RSS feed AddTextNode(xmlDoc, channelElement, "title", "Your RSS Feed Title"); AddTextNode(xmlDoc, channelElement, "description", "Your RSS Feed Description"); AddTextNode(xmlDoc, channelElement, "link", URLHelper.GetAbsoluteUrl("~/")); AddTextNode(xmlDoc, channelElement, "generator", "Kentico 13 RSS Generator"); // Iterate through the pages and add them to the RSS feed XML foreach (var page in pages) { var itemElement = xmlDoc.CreateElement("item"); // Customize item properties based on your page structure AddTextNode(xmlDoc, itemElement, "title", page.DocumentName); AddCDataNode(xmlDoc, itemElement, "description", page.DocumentContent); // Adding CDATA for description AddTextNode(xmlDoc, itemElement, "pubDate", page.DocumentPublishTo.ToString("ddd, dd MMM yyyy", CultureInfo.InvariantCulture)); // Format date AddTextNode(xmlDoc, itemElement, "link", URLHelper.GetAbsoluteUrl(page.NodeAliasPath)); channelElement.AppendChild(itemElement); } return Content(xmlDoc.OuterXml); } // Helper method to add a text node to an element in the XmlDocument private void AddTextNode(XmlDocument xmlDoc, XmlElement parentElement, string nodeName, string nodeValue) { var element = xmlDoc.CreateElement(nodeName); var textNode = xmlDoc.CreateTextNode(nodeValue); element.AppendChild(textNode); parentElement.AppendChild(element); } // Helper method to add a CDATA section node to an element in the XmlDocument private void AddCDataNode(XmlDocument xmlDoc, XmlElement parentElement, string nodeName, string nodeValue) { var element = xmlDoc.CreateElement(nodeName); var cdata = xmlDoc.CreateCDataSection(nodeValue); element.AppendChild(cdata); parentElement.AppendChild(element); }
The AddTextNode method is a helper function used to create an element with a specific node name and add it to a parent element in the XmlDocument.
The helper method AddCDataNode creates a CDATA section for specified elements (title and description in this case). This allows the content within these elements to be treated as character data, preserving any special characters or HTML tags that might be present in the Kentico page content.
The AddCDataNode helper method is used to encapsulate the ‘description’ element within a CDATA section, while other elements are added as text nodes. Adjust the logic as per your specific requirements and extend this approach if you need to include CDATA for other elements as well.
The ToString() method with a custom date format specifier ddd, dd MMM yyyy is used to format the date as a standard RSS date: “Thu, 27 Apr 2023”. Adjust the format specifier as needed to match the desired date format.
The above code demonstrates the construction of an RSS feed using XmlDocument and related methods. It creates an XML structure by appending elements (XmlElement) and text nodes to represent the various components of the RSS feed, such as rss, channel, title, description, item, etc.
This approach provides a structured and maintainable way to generate XML compared to manual string concatenation.
Adjust the properties and structure according to your Kentico pages and desired RSS feed format.
Register route endpoint for RSS feed
To display the RSS feed on your Kentico site you might need to register a route endpoint in the RouteConfig.cs.
Usually, you can find RouteConfig.cs under App_Start folder of your application.
Example:
endpoints.MapControllerRoute( name: "RSSNews", pattern: "RSSNews.xml", defaults: new { controller = "XML", action = "RSSNews" } );
If it is done properly, your RSS feed should be available here:
https://YourSite.com/RSSNews.xml