Creating a PublishNavigation JSON TBB in DXA

December 3rd, 2017 | Posted by Robert Curlette in DXA

DXA 1.7 includes Template Building Blocks that help us publish content in JSON format to the Tridion Broker database. One of these Template Building Blocks is the GenerateSitemap.tbb that publishes all Pages and Structure Groups from your entire website as 1 JSON file. You might think this sounds great – and it really is – one great big file! However, if you have thousands of Pages and hundreds of Structure Groups, you might just be interested to publish the Structure Group info and Index pages in the Navigation JSON file. In this short post I’ll share the code I used to get started.  If this is your first time hacking the DXA Template Building Blocks, you might want to check out my post here on how to get started compiling the DXA TBBs.

The idea here is to create a new TBB, GenerateNavigation tbb, that publishes just the Structure Groups and index pages.

I’ve re-used the entire GenerateSitempa.tbb file and just modified one of the methods.

Here is my new modified method and also the call to it.

 

public override void Transform(Engine engine, Package package)
{
    Initialize(engine, package);

    _config = GetNavigationConfiguration(GetComponent());
   
    SitemapItem sitemap = GenerateStructureGroupNavigation(Publication.RootStructureGroup, true);

    string sitemapJson = JsonSerialize(sitemap);

    package.PushItem(Package.OutputName, package.CreateStringItem(ContentType.Text, sitemapJson));
}

 

private SitemapItem GenerateStructureGroupNavigation(StructureGroup structureGroup, bool structureGroupsOnly)
{
    SitemapItem result = new SitemapItem
    {
        Id = structureGroup.Id,
        Title = GetNavigationTitle(structureGroup),
        Url = System.Web.HttpUtility.UrlDecode(structureGroup.PublishLocationUrl),
        Type = ItemType.StructureGroup.ToString(),
        Visible = IsVisible(structureGroup.Title)
    };

   
    foreach (RepositoryLocalObject item in structureGroup.GetItems().Where(i => !i.Title.StartsWith("_")).OrderBy(i => i.Title))
    {
        SitemapItem childSitemapItem = null;
        Page page = item as Page;
        if (page != null)
        {
            if (page.FileName == "index")  // Add pages with the name index - APA Custom
            {
                if (!IsPublished(page))
                {
                    continue;
                }

                childSitemapItem = new SitemapItem
                {
                    Id = page.Id,
                    Title = GetNavigationTitle(page),
                    Url = GetUrl(page),
                    Type = ItemType.Page.ToString(),
                    PublishedDate = GetPublishedDate(page, Engine.PublishingContext.TargetType),
                    Visible = IsVisible(page.Title)
                };
            }
        }
        else
        {
            childSitemapItem = GenerateStructureGroupNavigation((StructureGroup)item, true);
        }
        if(childSitemapItem != null)
        {
            result.Items.Add(childSitemapItem);
        }
    }
   
    return result;
}

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

One Response

  • Hi Robert,

    Great idea. Only where is the param structureGroupsOnly used?

    My suggestion would be to use a parameter schema to toggle between Full structure / SG only structure instead of hardcoded ‘true’. Then it is generated enough for a DXA pull request.



Leave a Reply

Your email address will not be published. Required fields are marked *