Find usages of images from code



I have, from code, moved some images from different folders to one and I hoped that name conflicts whould be resolved by overwriting. That wasn't the case, instead there are two or more "versions" of the same file but with different ContentReferences. Then I want to delete the duplicated files that are not referenced by a page but I'm not sure which one that's used (it differs). When I remove an image in edit mode I get a warning if the image is used by a page, but from code I don't find a way to findout which pages that use a specific image. Does anyone know how to do that?

Jun 10, 2015 16:20

I think you could go with:

var softLinkRepos = ServiceLocator.Current.GetInstance<ContentSoftLinkRepository>();
            var softLinks = softLinkRepos.Load(contentLink, true);

If I understood the question correct. :)

Jun 10, 2015 16:52

Hi Erik,

Thank you, I've tried the ContentSoftLinkRepository without success, I get 0 SoftLinks back. I'm testing with two images where one of them is used on a page. But both images and the page have 0 softlinks and I try three variants
_contentSoftLinkRepository.Load(content.ContentGuid, true)
_contentSoftLinkRepository.Load(content.ContentGuid, false)
_contentSoftLinkRepository.Load(content.ContentLink).But no result from any of them.

Jun 11, 2015 7:36

Apperently there is a method on the IContentRepository called GetReferencesToContent. Try that one instead. :)

var references = ServiceLocator.Current.GetInstance<IContentRepository>().GetReferencesToContent(page.ContentLink, false);

Jun 11, 2015 10:49

That did the trick, thank you Erik!

Jun 11, 2015 13:40
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.