-1

I want to get all the Sitecore media items on the current page. This could be an image, video, pdf ,mp3 or docx

Has anyone done something similar to this?

What i am currently doing is

  • Getting all the renderings on the current item.
  • Each rendering has a datasource. So getting a datasource item.
  • Now checking each field on the datasource item and if it is a media item then get the Keywords field value.

But I think this will have a performance issue. Or is there any better way of doing this?

Any help or suggestion would be appreciated.

Thanks in advance

  • Why? perhaps a Front end solution scanning the DOM on /-/media – Jan Bluemink May 1 '19 at 8:57
  • @JanBluemink each media item has a keyword field so i want to get all the media items on the current page -> get the media item -> read Keyword value field -> add that in the metadata – Owais Ahmed May 1 '19 at 9:00
  • 2
    Meta Keywords have not been used in over 9 years. My 2 cents, given the time it will take to implement this versus the zero business value - someone isn't thinking straight :P datadrivenlabs.io/blog/meta-keyword-tag-google-2018 – Mark Cassidy May 1 '19 at 10:52
  • Closing this. First of all it's crossposted on SO. Secondly; for amending the question once an answer has been given - to now ask for something completely different. – Mark Cassidy May 1 '19 at 15:36
3

The LinkDatabase can help you with this.

  1. Get all references from your Sitecore.Context.Item, add any items under /sitecore/media library to a list.
  2. Get all references from each reference, filter out anything that isn't under /sitecore/media library and add the remaining to your list
  3. Now loop through your list and collect all your keywords
  4. Add them to meta data

For specific code examples, see Effective use of Sitecore LinkDatabase.

| improve this answer | |
  • thanks for the reply. What I am doing is getting all the renderings on the current item and each rendering has a datasource. I am checking each field on the datasource item and if it is a media item then get the Keywords field value. But I think this will have a performance issue. – Owais Ahmed May 1 '19 at 12:46
  • Yes. My approach will work faster. And you arrive at the same result - if there IS a reference to a media item ANYWHERE in relation to your context item, my algorithm outlined above will find them. Quick, too. – Mark Cassidy May 1 '19 at 12:54
  • sorry what do you mean by Get all references from Sitecore.Context.Item? – Owais Ahmed May 1 '19 at 12:55
  • how to get all the references? – Owais Ahmed May 1 '19 at 13:03
  • Via the LinkDatabase. The article I linked explains how it can be used in detail. – Mark Cassidy May 1 '19 at 13:44

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