0

So we already have a custom site map generation service in our Sitecore 9.2 solution, to generate standard sitemap.xml files, which is working absolutely perfectly. However, we now want to extend it to include image/video/assets as well along the lines of https://developers.google.com/search/docs/advanced/sitemaps/image-sitemaps.

Is there a way, programmatically, to find all the media library items that the current published version of an item uses (including all the components, placeholders, etc. that are on the page) ? Or are we going to have to use a HTML scraper on every page in the sitemap XML file ?

1 Answer 1

1

If you want to find all the items that are in the published state then you need to check in your web database. You can write the logic in code like this.

First, you need to get all the media items that are published or in web database.

var mediaLibrary = Sitecore.Data.Database.GetDatabase("web").GetItem("/sitecore/media library");
var childItems = mediaLibrary.GetChildren(); 

If you want to get all levels of child items then you need to use Axes.GetDescendants();

Now you need to create a method that will show you if the media item or item is being used in any other Sitecore item?

public bool GetReferrers(Item item)
{
   // getting all linked Items that refer to the Item
   ItemLink[] itemLinks = Globals.LinkDatabase.GetReferrers(item);

   if (itemLinks == null)
       return false;
   
   return true;   
}

By using this approach you can get all the list of items that are being referred in any Sitecore item.

This is just a starting point and you can extend your code as per your requirement.

Hope this will help.

Thanks

4
  • Thanks for the reply. Does GetReferrers() find all items that use the media item in any way ? Or only items that directly relate to it via, say, an image field on the item ? Our issue is that many/most of our pages contain multiple components, sub-views, renderings, placeholders. etc which can themselves contain/relate to media library item i.e the item itself has no image fields on it at all but when rendered the displayed page could contain any number of images. We need to find all of these media items as well
    – Adam Brunt
    Jul 7, 2022 at 12:03
  • @AdamBrunt The GetReferrers() method will give you a list of items that are using the item you are passing in the method. Using the Sitecore Link Database you can find the linking of the items. And as I mentioned that this is the starting point of the logic. You will get the items then you may need to extend your logic as per your requirement. Jul 7, 2022 at 12:15
  • Ah, ok., Thanks. I misread your first answer and thought GetReferrers() only accepted media libray items. So getrefferers tells me which components/placeholders/etc is using a particular media item; I would then need to work out whether those items are used by the item I am interested in ? Finally, hopefully, is it also possible to restrict the GetItem call to certain asset types say JPGs and PDFs ?
    – Adam Brunt
    Jul 7, 2022 at 12:23
  • Yes if the function GetReferrers() returns true then the item you are passing is in use. So you can extend your logic with that. Jul 7, 2022 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.