Example #1
0
        /// <summary>
        /// The build sitemap index.
        /// </summary>
        /// <param name="fileNames">
        /// The file names.
        /// </param>
        /// <param name="site">
        /// The site.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private string BuildSitemapIndex(IEnumerable <string> fileNames, SiteDefinition site)
        {
            var serverUrl = SitemapManagerConfiguration.GetServerUrlBySite(site);

            var doc             = new XmlDocument();
            var declarationNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            doc.AppendChild(declarationNode);

            var siteMapIndexNode = doc.CreateElement("sitemapindex");
            var xmlNsAttr        = doc.CreateAttribute("xmlns");

            xmlNsAttr.Value = SitemapManagerConfiguration.XmlNsTpl;
            siteMapIndexNode.Attributes.Append(xmlNsAttr);
            doc.AppendChild(siteMapIndexNode);

            foreach (var filename in fileNames)
            {
                var url     = $"{serverUrl}/{ HttpUtility.HtmlEncode(filename.Replace("\\", "/"))}";
                var lastMod = HttpUtility.HtmlEncode(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));

                var sitemapNode = doc.CreateElement("sitemap");
                siteMapIndexNode.AppendChild(sitemapNode);

                var locNode = doc.CreateElement("loc");
                sitemapNode.AppendChild(locNode);
                locNode.AppendChild(doc.CreateTextNode(url));

                var lastModNode = doc.CreateElement("lastmod");
                sitemapNode.AppendChild(lastModNode);
                lastModNode.AppendChild(doc.CreateTextNode(lastMod));
            }

            return(doc.OuterXml);
        }
Example #2
0
        /// <summary>
        /// The register to robots fields.
        /// </summary>
        public void RegisterToRobotsFields()
        {
            foreach (DictionaryEntry site in this.sites)
            {
                var siteName    = site.Key.ToString();
                var defRep      = ServiceLocator.Current.GetInstance <ISiteDefinitionRepository>();
                var siteDef     = defRep.Get(siteName);
                var serverUrl   = SitemapManagerConfiguration.GetServerUrlBySite(siteDef);
                var sitemapLine = string.Concat("Sitemap: ", serverUrl, "sitemap.xml");

                var rep       = ServiceLocator.Current.GetInstance <IContentRepository>();
                var startPage = rep.Get <PageData>(siteDef.StartPage);

                // Save robots content from field
                if (startPage == null)
                {
                    continue;
                }

                var robotsFromField         = string.Empty;
                var robotsFromFieldProperty = startPage.GetType().GetProperty("RobotsContent");
                if (robotsFromFieldProperty?.GetValue(startPage) is string)
                {
                    robotsFromField = robotsFromFieldProperty.GetValue(startPage) as string;
                }

                if (string.IsNullOrEmpty(robotsFromField) || !robotsFromField.Contains(sitemapLine))
                {
                    robotsFromField = sitemapLine + Environment.NewLine;
                    var startPageWritable = ((PageData)startPage).CreateWritableClone();

                    if (startPageWritable != null)
                    {
                        var robotsFromFieldPropertyWritable = startPageWritable.GetType().GetProperty("RobotsContent");
                        robotsFromFieldPropertyWritable?.SetValue(startPageWritable, robotsFromField);
                        rep.Save(
                            (PageData)startPageWritable,
                            EPiServer.DataAccess.SaveAction.Publish,
                            EPiServer.Security.AccessLevel.NoAccess);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// The register to robots file.
        /// </summary>
        public void RegisterToRobotsFile()
        {
            var robotsPath     = System.Web.Hosting.HostingEnvironment.MapPath(string.Concat("/", "robots.txt"));
            var sitemapContent = new StringBuilder(string.Empty);

            if (robotsPath == null)
            {
                return;
            }

            if (File.Exists(robotsPath))
            {
                var sr = new StreamReader(robotsPath);
                sitemapContent.Append(sr.ReadToEnd());
                sr.Close();
            }

            var sw = new StreamWriter(robotsPath, false);

            foreach (DictionaryEntry site in this.sites)
            {
                var siteName    = site.Key.ToString();
                var filename    = site.Value.ToString();
                var defRep      = ServiceLocator.Current.GetInstance <ISiteDefinitionRepository>();
                var siteDef     = defRep.Get(siteName);
                var serverUrl   = SitemapManagerConfiguration.GetServerUrlBySite(siteDef);
                var sitemapLine = string.Concat("Sitemap: ", serverUrl, SitemapSubFolder, "/", filename);

                if (!sitemapContent.ToString().Contains(sitemapLine))
                {
                    sitemapContent.AppendLine(sitemapLine);
                }
            }

            sw.Write(sitemapContent.ToString());
            sw.Close();
        }