Wednesday, May 22, 2019

Make Sitecore SXA Sitemap Generation Respect SiteLanguages Setting

Recently we faced an issue where Sitecore SXA Sitemap is not respecting the Languages selected from the "Site Languages" field in site settings item.


For example, say you have a SXA site with "en-GB" selected as the Site Language in the settings. (see image below).



But  if an item have "en" language item also, generated sitemap will contain "hreflang" entries for both "en-GB" and "en" languages. (see image below)



So I decoded the Sitecore.XA.Feature.SiteMetadata.dll and in the "BuildMultilanguageSitemap" method, it's taking all the languages of the item and adding "hreflang" for each of those languages.
As you can see in the below image, it doesn't consider "Site Languages" setting set in the "Settings" item.




public class SitemapGenerator : Sitecore.XA.Feature.SiteMetadata.Sitemap.SitemapGenerator
{
    {
        private _MultiLanguageSettings _multiLanguageSettings;

        public SitemapGenerator() : base()
        {
            _multiLanguageSettings = ServiceLocator.ServiceProvider.GetService<IMultiLanguageRepository>().GetMultiLanguageSettings(Sitecore.Context.Site);
        }

        protected override StringBuilder BuildMultilanguageSitemap(IEnumerable<Item> childrenTree, SitemapLinkOptions options)
        {
            UrlOptions urlOptions = this.GetUrlOptions();
            SitemapLinkOptions options1 = new SitemapLinkOptions(options.Scheme, urlOptions, options.TargetHostname);
            List<XElement> xelementList1 = new List<XElement>();
            StringBuilder stringBuilder = new StringBuilder();

            if (_multiLanguageSettings == null || _multiLanguageSettings.SiteLanguages == null || !_multiLanguageSettings.SiteLanguages.Any())
            {
                return stringBuilder;
            }

            foreach (Item item in childrenTree)
            {
                SitemapChangeFrequency sitemapChangeFrequency = item.Fields[Sitecore.XA.Feature.SiteMetadata.Templates.Sitemap._Sitemap.Fields.ChangeFrequency].ToEnum<SitemapChangeFrequency>();

                if (sitemapChangeFrequency != SitemapChangeFrequency.DoNotInclude)
                {
                    List<XElement> xelementList2 = new List<XElement>();

                    foreach (Language language in item.Languages)
                    {
                        if (!_multiLanguageSettings.SiteLanguages.Any(l => l.Name.Equals(language.Name, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            continue;
                        }

                        Item langItem = item.Database.GetItem(item.ID, language);
                        if (langItem.Versions.Count > 0)
                        {
                            options1.UrlOptions.Language = language;
                            XElement xelement = this.BuildAlternateLinkElement(this.GetFullLink(langItem, options1), language.CultureInfo.Name, "alternate");
                            xelementList2.Add(xelement);
                        }
                    }

                    options1.UrlOptions.Language = item.Language;
                    XElement xelement1 = this.BuildPageElement(this.GetFullLink(item, options1), this.GetUpdatedDate(item), sitemapChangeFrequency.ToString().ToLower(), this.GetPriority(item), (IEnumerable<XElement>)xelementList2);
                    xelementList1.Add(xelement1);
                }
            }

            XDocument xdocument = this.BuildXmlDocument((IEnumerable<XElement>)xelementList1);

            using (TextWriter textWriter = (TextWriter)new StringWriter(stringBuilder))
            {
                xdocument.Save(textWriter);
            }

            this.FixDeclaration(stringBuilder);
            return stringBuilder;
        }
}


Hope this helps !!!

No comments:

Post a Comment