Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Find page an asset is on

Vote:
 

Hi,

Is there a way in c# to find the page(s) that a certain asset such as an image is on albeit a page or block on a page. Ideally I would like an asset search library and allows the user to click on the a link that will take them to the image on the page.

Thanks

Jon

#144998
Feb 23, 2016 16:20
Vote:
 

Check out softlink class in api to get references.

http://world.episerver.com/documentation/Class-library/?documentId=cms/8/2244434

#145002
Feb 23, 2016 16:44
Vote:
 

That will give you a list of softlinks that contain pagelink as property. Use UrlResolver to get the url from that...

#145003
Feb 23, 2016 16:47
Vote:
 

Hi Jonathan,

If you're using the latest version of EPiServer CMS, you can try the following code:

var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var content = contentRepository.Get<ImageData>(new ContentReference(87));
var references = contentRepository.GetReferencesToContent(content.ContentLink, true).ToList();

I used IContentSoftLinkRepository for Blocks and Pages, but it doesn't return correct results for Assets.

#145008
Feb 23, 2016 18:11
Vote:
 

Great - but how would you dynamically know what Data Type the asset is? For example Video, Pdf, Image etc for this bit of the code: var content = contentRepository.Get<???>(new ContentReference(87));

#145009
Feb 23, 2016 18:18
Vote:
 

You can fetch the content as IContent

#145018
Feb 23, 2016 19:28
Vote:
 

.Get<MediaData>(mediaReference) as Image data

Or

.Get<MediaData>(mediaReference).MimeType 

are two ways to get what type of media content you get...or use IContent like Dejan says if you don't even know if it is media.

#145020
Feb 23, 2016 21:18
Vote:
 

Hi, These are all great replies and work - unfortunately it is very slow as I have lots of assets and lots of pages - it takes about 1 minute to get all the data back.

I think ill give this idea a miss - but thanks for all your help guys,

Jon

#145079
Feb 24, 2016 10:54
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.