Generate Sitemap XML File using default functions of DXA 1.5/1.7 in Web 8/8.5 (Tridion)

As discussed in Last Blog about Generating the Sitemap XML for one of the customer and it's environment is with DXA 1.7 with Web8.5, Hence Forth the Earlier Sitemap Generation using TBB didn't work and after figuring out in Many ways , deep dive into default DXA Code and figured out that by Default DXA provides the Navigation (in Json Format) and Sitemap Generation as well... 

So i have used the same to implement / generate the SiteMap Xml file from that base Code - it's very simple to use and below are the Steps:

1) Create a Page Template "Navigate XML" and Output will be XML and add the SiteMapXml  View in the "Page View Name" Section

Below you can see the Image:



by Default in DXA , from the PageModel , we get the SitemapItem from the below context

SiteConfiguration.NavigationProvider.GetNavigationModel(WebRequestContext.Localization)

after getting the sitemapItem convert to the same into your Project Specific Model and pass the same to the @SitemapForPagesInFolder(XXXSitemapmodels) so that this is a default method Provided by DXA where you can get all the items, then you can play with this data and rendering the same in xml file.

Here is the Pic describes in More Clarity:

 

Here we have used "X-Forwarded-Proto" since one the code is Production Server where Internally LoadBalancer sometimes redirect to Http instead of Https which we can't control, so in order to fix the same we are checking "Scheme" in the Request.Headers.

Finally just create the Page with the above "Navigation XML" PT and no need to pass any Component and just publish the same.

Imp Note: since the we are getting the info from "NavigationProvider.GetNavigationModel" it is Mandatory to Publish the Navgation.json file so that it index all the page currently published. so that we will get all the required pages for Sitemap as well...

Hope this simple logic will be useful for many  more customers... Waiting for any suggestion and Feedback as well...

Thank you,

Shalivahan K