Retrieve Pages Based on Certain Tags and Categories

retrieve-pages-based-on-certain-tags-and-categories kentico

In Kentico 13, you can use the MultiDocumentQuery or DocumentQuery class to retrieve pages based on certain tags and categories.

Using MultiDocumentQuery

using CMS;
using CMS.DocumentEngine;
using CMS.SiteProvider;

// Get the current site
SiteInfo currentSite = SiteContext.CurrentSite;

// Define the tag names and category names to filter pages
string[] tagNames = { "Tag1", "Tag2" };
string[] categoryNames = { "Category1", "Category2" };

// Create a MultiDocumentQuery for pages
MultiDocumentQuery query = DocumentHelper.GetDocuments()
    .Path("/YourPagePathHere/", PathTypeEnum.Children) // Specify the path to search for pages
    .OnSite(currentSite.SiteName) // Set the site context
    .Type("cms.page") // Filter by page type
    .Published(true); // Filter only published pages

// Filter pages by specified tags
if (tagNames.Length > 0)
{
    query = query.WhereContains("DocumentTags", tagNames);
}

// Filter pages by specified categories
if (categoryNames.Length > 0)
{
    query = query.WhereContains("DocumentCategories", categoryNames);
}

// Retrieve the resulting pages
InfoDataSet<PageInfo> pages = query.TypedResult;

// Iterate through the pages
if (!DataHelper.DataSourceIsEmpty(pages))
{
    foreach (PageInfo page in pages)
    {
        // Access page properties or perform actions here
        string pageTitle = page.DocumentName;
        string pageUrl = page.NodeAliasPath;

        // Output or process the retrieved pages
        // Example: Console.WriteLine("Page Title: " + pageTitle + ", URL: " + pageUrl);
    }
}

Replace “/YourPagePathHere/” with the path where you want to search for pages. Modify tagNames and categoryNames arrays to include the specific tags and categories you want to filter by.

This code retrieves pages based on the specified path, tags, and categories using Kentico’s MultiDocumentQuery. You can further process the retrieved pages according to your requirements, such as displaying their titles, URLs, or performing additional actions.

Using DocumentQuery

Kentico provides a robust API for querying pages based on tags and categories using the DocumentHelper and its Query methods. The DocumentQuery API is an alternative and cleaner way to achieve this. Here’s an example:

using CMS.DocumentEngine;
using CMS.SiteProvider;

// Get the current site
SiteInfo currentSite = SiteContext.CurrentSite;

// Define the tag names and category names to filter pages
string[] tagNames = { "Tag1", "Tag2" };
string[] categoryNames = { "Category1", "Category2" };

// Create a document query for pages
DocumentQuery query = DocumentHelper.GetDocuments()
    .Path("/YourPagePathHere/", PathTypeEnum.Children) // Specify the path to search for pages
    .OnSite(currentSite.SiteName) // Set the site context
    .Types("cms.page") // Filter by page type
    .Published(true); // Filter only published pages

// Filter pages by specified tags
if (tagNames.Length > 0)
{
    query = query.WhereIn("DocumentID", 
        DocumentHelper.GetDocuments("cms.tag")
            .WhereIn("TagName", tagNames)
            .Column("NodeID")
    );
}

// Filter pages by specified categories
if (categoryNames.Length > 0)
{
    query = query.WhereIn("DocumentID", 
        DocumentHelper.GetDocuments("cms.category")
            .WhereIn("CategoryName", categoryNames)
            .Column("NodeID")
    );
}

// Retrieve the resulting pages
IEnumerable<TreeNode> pages = query.TypedResult;

// Iterate through the pages
foreach (TreeNode page in pages)
{
    // Access page properties or perform actions here
    string pageTitle = page.DocumentName;
    string pageUrl = DocumentURLProvider.GetUrl(page);

    // Output or process the retrieved pages
    // Example: Console.WriteLine("Page Title: " + pageTitle + ", URL: " + pageUrl);
}

Similar to the previous code, replace “/YourPagePathHere/” with the desired path and modify tagNames and categoryNames arrays to include the specific tags and categories you want to filter by.

This code utilizes Kentico’s DocumentQuery API and the DocumentHelper methods to construct a cleaner query for retrieving pages based on specified paths, tags, and categories. It then iterates through the retrieved pages, allowing you to access their properties or perform necessary actions as needed.