Exemple #1
0
        /// <summary>
        /// Reads configurations from Sitecore
        /// </summary>
        public virtual void ReadConfigurations()
        {
            ReadGlobalSitecoreConfiguration();

            SiteConfigurations = new List <SitemapSiteConfiguration>();

            if (SitecoreConfiguration.DisableSitemap)
            {
                Sitecore.Diagnostics.Log.Warn(Messages.SitemapDisabled, this);
                return;
            }

            Item[] configurationItems = Database.SelectItems(DynamicSitemapConfiguration.SitemapConfigurationItemPath + DynamicSitemapConfiguration.SitemapConfigurationSitesFolderName + "/*[@@templateid='" + TemplateIds.SiteConfigurationTemplateId + "']");

            if (!configurationItems.Any())
            {
                Sitecore.Diagnostics.Log.Warn(Messages.NoConfigurations, this);
                return;
            }

            if (!SitecoreConfiguration.ProcessedLanguages.Any())
            {
                Sitecore.Diagnostics.Log.Warn(Messages.NoProcessedLanguages, this);
                return;
            }

            foreach (var configurationItem in configurationItems)
            {
                var languageItems = configurationItem.Languages.Where(x => SitecoreConfiguration.ProcessedLanguages.Contains(x.Name)).ToList();

                foreach (var languageItem in languageItems)
                {
                    var item = configurationItem.Database.GetItem(configurationItem.ID, languageItem);

                    if (item.Versions.Count > 0)
                    {
                        var site = configurationItem.Name.ToLower();

                        var sitemapSiteConfiguration = new SitemapSiteConfiguration(item);

                        sitemapSiteConfiguration.SitemapFileName = sitemapSiteConfiguration.SitemapFileName != String.Empty
                            ? sitemapSiteConfiguration.SitemapFileName
                            : String.Format(_sitemapFileNameFormat, site, languageItem.Name.ToLower());

                        sitemapSiteConfiguration.SitemapFilePath = DynamicSitemapConfiguration.SitemapConfigurationOutputFolder + "/" + sitemapSiteConfiguration.SitemapFileName;

                        // - Load ItemsProcessor -

                        if (!String.IsNullOrWhiteSpace(sitemapSiteConfiguration.ItemsProcessorTypeToLoad))
                        {
                            var loader         = new ItemsProcessorLoader();
                            var itemsProcessor = loader.Load(sitemapSiteConfiguration.ItemsProcessorTypeToLoad);

                            if (itemsProcessor != null)
                            {
                                sitemapSiteConfiguration.ItemsProcessor = itemsProcessor;
                            }

                            else
                            {
                                Sitecore.Diagnostics.Log.Warn(String.Format(Messages.CannotLoadItemsProcessor, sitemapSiteConfiguration.ItemsProcessorTypeToLoad), this);
                            }
                        }

                        SiteConfigurations.Add(sitemapSiteConfiguration);
                    }
                }
            }

            SitecoreConfiguration.MainSiteConfiguration = SiteConfigurations.FirstOrDefault(x => String.Equals(x.Site.Name, SitecoreConfiguration.MainSiteConfigurationItem.Name, StringComparison.CurrentCultureIgnoreCase));

            if (SitecoreConfiguration.MainSiteConfiguration == null)
            {
                Sitecore.Diagnostics.Log.Warn("No main site configuration", this);
            }

            SitemapIndex = new SitemapIndexConfiguration
            {
                ServerHost = SitecoreConfiguration.MainSiteConfiguration != null
                    ? SitecoreConfiguration.MainSiteConfiguration.ServerHost
                    : SiteConfigurations.FirstOrDefault()?.ServerHost,
                FileName = _sitemapIndexFileName
            };
        }
        /// <summary>
        /// Reads configurations from Sitecore
        /// </summary>
        public void ReadConfigurations()
        {
            this.ReadGlobalSitecoreConfiguration();

            Item[] configurationItems = this.Database.SelectItems(DynamicSitemapConfiguration.SitemapConfigurationItemPath + DynamicSitemapConfiguration.SitemapConfigurationSitesFolderName + "/*[@@templateid='" + TemplateIds.SiteConfigurationTemplateId + "']");

            if (configurationItems.Count() == 0)
            {
                Diagnostics.Log.Warn(Messages.NoConfigurations, this);
                return;
            }

            this.SiteConfigurations = new List <SitemapSiteConfiguration>();

            foreach (var configurationItem in configurationItems)
            {
                var languageItems = configurationItem.Languages.Where(x => this.SitecoreConfiguration.ProcessedLanguages.Contains(x.Name)).ToList();

                foreach (var languageItem in languageItems)
                {
                    var item = configurationItem.Database.GetItem(configurationItem.ID, languageItem);

                    if (item.Versions.Count > 0)
                    {
                        var site = configurationItem.Name.ToLower();

                        var sitemapSiteConfiguration = new SitemapSiteConfiguration(item);

                        sitemapSiteConfiguration.SitemapFileName = sitemapSiteConfiguration.SitemapFileName != String.Empty
                            ? sitemapSiteConfiguration.SitemapFileName
                            : String.Format(this._sitemapFileNameFormat, site, languageItem.Name.ToLower());

                        sitemapSiteConfiguration.SitemapFilePath = DynamicSitemapConfiguration.SitemapConfigurationOutputFolder + "/" + sitemapSiteConfiguration.SitemapFileName;

                        // - Load ItemsProcessor -

                        if (!String.IsNullOrWhiteSpace(sitemapSiteConfiguration.ItemsProcessorTypeToLoad))
                        {
                            var loader         = new ItemsProcessorLoader();
                            var itemsProcessor = loader.Load(sitemapSiteConfiguration.ItemsProcessorTypeToLoad);

                            if (itemsProcessor != null)
                            {
                                sitemapSiteConfiguration.ItemsProcessor = itemsProcessor;
                            }

                            else
                            {
                                Diagnostics.Log.Warn(String.Format(Messages.CannotLoadItemsProcessor, sitemapSiteConfiguration.ItemsProcessorTypeToLoad), this);
                            }
                        }

                        this.SiteConfigurations.Add(sitemapSiteConfiguration);
                    }
                }
            }

            this.SitecoreConfiguration.MainSiteConfiguration = this.SiteConfigurations.FirstOrDefault(x => x.Site.Name.ToLower() == this.SitecoreConfiguration.MainSiteConfigurationItem.Name.ToLower());

            this.SitemapIndex = new SitemapIndexConfiguration();

            var siteConfiguration = this.SitecoreConfiguration.MainSiteConfiguration ?? this.SiteConfigurations.FirstOrDefault();

            if (siteConfiguration != null)
            {
                this.SitemapIndex.ServerHost     = siteConfiguration.ServerHost;
                this.SitemapIndex.ForceHttps     = siteConfiguration.ForceHttps;
                this.SitemapIndex.TargetHostName = siteConfiguration.TargetHost;
            }
            this.SitemapIndex.FileName = this._sitemapIndexFileName;
        }