Finding all content a Page is referencing

Vote:
 

I am wondering if there is a way to find out all content that a content is referencing (not being referenced by which can be found by using GetReferencesToContent).

#203551
Apr 26, 2019 14:59
Vote:
 

- You can use IContentSoftLinkRepository.Load(ContentReference contentLink, bool reversed) to find the SoftLink (basically links that are in contentarea for example, IIRC), and then iterate over the Property to check if there is any ContentReference property and where it is pointing to.

#203552
Apr 26, 2019 15:32
Vote:
 

Dynamically looping through all the properties although you'd have to look in to content areas and any blocks if you wanted to go beyond the page. I think there's been blog or fourm with example core for this 

#203553
Apr 26, 2019 15:32
Vote:
 

Hi Robert,

There is nothing out-of-the-box.

Combining what Quan and Scott are saying you'd have to do something like this:

public ICollection<ContentReference> GetReferencesFromContent(ContentReference contentLink)
{
    // Service locator used purely for example purposes :)
    var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
    var contentSoftLinkRepository = ServiceLocator.Current.GetInstance<IContentSoftLinkRepository>();

    var content = contentLoader.Get<IContent>(contentLink);

    var contentLinks = new List<ContentReference>();

    foreach (var property in content.Property)
    {
        // Get straight content references
        if (property.PropertyValueType == typeof(ContentReference))
        {
            var link = property.Value as ContentReference;

            if (!ContentReference.IsNullOrEmpty(link))
            {
                contentLinks.Add(link);
            }
        }

        // Get IList<ContentReference> links
        if (typeof(IEnumerable<ContentReference>).IsAssignableFrom(property.PropertyValueType))
        {
            var links = property.Value as IEnumerable<ContentReference>;

            if (links != null)
            {
                contentLinks.AddRange(links);
            }
        }
    }

    // Get links from ContentAreas, LinkItemCollection and XhtmlStrings
    var softLinks = contentSoftLinkRepository.Load(contentLink).Where(x =>
        x.SoftLinkType == ReferenceType.PageLinkReference || x.SoftLinkType == ReferenceType.ImageReference);

    contentLinks.AddRange(softLinks.Select(x => x.ReferencedContentLink));

    return contentLinks.Distinct().ToList();
}

Definitely don't think this is comprehensive, but hopefully it gives you a starting point.

#203554
Edited, Apr 26, 2019 16:28
Vote:
 

Thank you for your replies. I will try it out :)

#203632
Apr 30, 2019 12:22
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.