I am currently working on image sitemap generation in sitecore.So i need all the images used in a particular url of a website.
Here i need to get the details of all the items where media item is used.. or else i need to find what are all the media items(images) used in an item(url) in sitecore.
I have tried to get the image field from an item and it is working fine but what i need is to get all the images that are used in the item which are added through presentation details.
Item currentitem = master.GetItem("/sitecore/content/International/Cars/New models/All new XC90");
public static string GetImageURL(Item currentItem)
{
string imageURL = string.Empty;
Sitecore.Data.Fields.ImageField imageField = currentItem.Fields["Image"];
if (imageField != null && imageField.MediaItem != null)
{
Sitecore.Data.Items.MediaItem image = new Sitecore.Data.Items.MediaItem(imageField.MediaItem);
imageURL = Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl(image));
}
return imageURL;
}
Since a page is made up of multiple components you would need to iterate through those, retrieve all the datasourced items and check the field values. Don't forget that images can also be placed in Rich Text fields as well.
In order to ensure that you capture all of these, you may be better making a WebClient call back to the site, essentially scraping the rendered HTML and then using HTMLAgilityPack/FizzlerEx/CsQuery to return all the images. You can filter then to only ones from the media library or a particular location if required.
using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;
//get the page
HtmlWeb web = new HtmlWeb();
HtmlDocument document = web.Load("http://example.com/requested-page");
HtmlNode page = document.DocumentNode;
//loop through all images on the page
foreach(HtmlNode item in page.QuerySelectorAll("img"))
{
var src = item.Attributes["src"].Value;
// do some stuff
}
If you only want to get images referenced from the Media Library then you can restrict the query:
foreach(HtmlNode item in page.QuerySelectorAll("img[src^='/-/media/']"))
{
//do stuff
...
}