Creating Image Sitemap for Sitecore site
I have to generate an image sitemap for multisite Sitecore application, but I can’t get any tool available online for creating unlimited pages image sitemap, So I have decided to do it programmatically.
Let's do it.
Step 1:
Add a reference to HtmlAgilityPack.dll
Step 2:
Add a new Item "SitemapTest" add assign the item to use(rendering/Layout ) that we going to create in Sitecore solution where we need to get the image sitemap.
Step 3:
Add the condition not to crawl this newly added view page by the code we going to do.
Step 4:
Add the following code for image sitemap generation in the new rendering/Layout file and add it to Sitemaptest Item.
List<Item>
itemsList = new List<Item>();
string[]
languages = { "en", "ar-ae", "zh-cn", "it-it", "ru-ru" };
string ItemUrl
= String.Empty;
foreach (var lang in languages)
{
using (new LanguageSwitcher(Sitecore.Globalization.Language.Parse(lang)))
{
var rootItem
= Sitecore.Context.Database.GetItem("/sitecore/content/SiteHOME",
Sitecore.Globalization.Language.Parse(lang));
itemsList.Add(rootItem);
rootItem.Children.ToList().ForEach(p => itemsList.Add(p));
//Starting
urlset
XmlTextWriter enwriter
= new XmlTextWriter("D:\\Sitemap\\sitemap-Images-" +
lang.ToString() + ".xml", System.Text.Encoding.UTF8);
enwriter.WriteStartDocument(true);
enwriter.Formatting = Formatting.Indented;
enwriter.Indentation = 2;
enwriter.WriteStartElement("urlset");
//Write the namespace
declaration.
enwriter.WriteAttributeString("xmlns",
null, null, "http://www.sitemaps.org/schemas/sitemap/0.9");
enwriter.WriteAttributeString("xmlns",
"image", null, "http://www.google.com/schemas/sitemap-image/1.1");
foreach (Item childs in itemsList)
{
Sitecore.Diagnostics.Log.Info("ITEM",
rootItem.Paths.FullPath);
Sitecore.Diagnostics.Log.Info("language",
rootItem.Language.Name);
if (Sitecore.Data.ID.IsID(childs.ID.ToGuid().ToString()))
{
Item item
= Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(childs.ID),
Sitecore.Globalization.Language.Parse(lang));
if (item
!= null && item.Name != "SitemapTest" &&
item.Versions.Count > 0)
{
LayoutItem layoutItem
= item.Visualization.GetLayout(Sitecore.Context.Device);
if (layoutItem
!= null)
{
Sitecore.Diagnostics.Log.Info("ITEM", item.Paths.FullPath);
var options
= LinkManager.GetDefaultUrlOptions();
options.AlwaysIncludeServerUrl = true;
options.SiteResolving = true;
options.LanguageEmbedding = LanguageEmbedding.Always;
options.LowercaseUrls = true;
ItemUrl = LinkManager.GetItemUrl(item,
options);
var document1
= new HtmlWeb().Load(ItemUrl);
var urls
= from r in document1.DocumentNode.Descendants("img")
select new
{
src = String.Format("{0}{1}", Request.Url.Host,
r.GetAttributeValue("src", "")),
caption =
r.GetAttributeValue("alt", "")
};
if (urls
!= null && urls.Count() > 0)
{
//starting
url //starting url
enwriter.WriteStartElement("url");
enwriter.WriteStartElement("loc");
enwriter.WriteString(ItemUrl.ToLower());
enwriter.WriteEndElement();
enwriter.WriteStartElement("lastmod");
enwriter.WriteString(DateTime.Now.ToShortDateString());
enwriter.WriteEndElement();
enwriter.WriteStartElement("changefreq");
enwriter.WriteString("monthly");
enwriter.WriteEndElement();
foreach (var r in urls)
{
enwriter.WriteStartElement("image:image");
enwriter.WriteStartElement("image:loc");
enwriter.WriteString(r.src);
enwriter.WriteEndElement();
enwriter.WriteStartElement("image:caption");
enwriter.WriteString(r.caption);
enwriter.WriteEndElement();
enwriter.WriteEndElement();
}
//closing
urlset
enwriter.WriteEndElement();
}
}
}
}
}
//Closing
URLS
enwriter.WriteEndElement();
enwriter.WriteEndDocument();
enwriter.Close();
}
}
Output:
That’s it 😊
Comments
Post a Comment