7

I have a use case where I need to get a list of all the component links for a given component on the CD side. The reason for this is that I need to track all the pages that the component is on.

My thought was to use ComponentLink class, which normally returns either the first resolved Component Link or the best resolved Component Link via the standard Dynamic Linking Algorithm, and I was hoping that there would be method that return all the links (that the Dynamic Linking Algorithm does it's priority-based resolution on). This method seems to exist as per the Java doc:

com.tridion.linking.ComponentLink

protected java.util.List<LinkInfo> getLinkInfo(int publicationId,
                                               int componentId)

Loads the ComponentLinkInfo for a component in a publication. The collection returned contains a list of ComponentLinkInfo objects. Parameters:

  • publicationId - The ID of the publication.
  • componentId - The ID of the component.

Returns: A Collection of ComponentLinkInfo objects if they could be found. If an error occurs or the information isn't available, an empty collection will be returned.

As you can see the method exists, but it's protected, not public. So we can't use it unless our custom class is in the same package as the ComponentLink class, i.e. com.tridion.linking.

My question is, how do I get the list of urls for a given published Component(Presentation)?

3 Answers 3

5

It does look like the getLinkInfo method might work, so all we need to do is overcome the protected access modifier. One approach is to create a custom class in the com.tridion.linking package and wrap the getLinkInfo method with a public access modifier, then use the wrapper class' public method. So essentially have the custom public method with the same signature as the protected one call the protected method:

package com.tridion.linking;

import java.util.List;
import com.tridion.storage.LinkInfo;
import com.tridion.linking.ComponentLink;

public class ComponentLinkWrapper extends ComponentLink {
    public ComponentLinkWrapper(int pubId) {
        super(pubId);
    }

    public List<LinkInfo> getLinkInfo(int publicationId, int componentId) {
        return this.getLinkInfo(publicationId, componentId);    
    }
}
1
  • 1
    Thank you, this worked! But I think there is a mistake in your answer. In getLinkInfo method, instead of calling this.getLinkInfo you have to call super.getLinkInfo. Sep 26, 2017 at 10:26
3

The hard part here is that Tridion specifically didn't make this public API in the ComponentLinker, but there are always the internal DAO classes to go to. Be aware that as these are not public API, they might change in a new release of tridion. This snippet would net you all links for a given id:

LinkInfoDAO linkInfoDao = (LinkInfoDAO) StorageManagerFactory.getDAO(publicationId, StorageTypeMapping.LINK_INFO);
TCMURI tcmUri = new TCMURI(componentId);
List<LinkInfo> cLinks = linkInfoDao.findByComponent(publicationId, tcmUri.getItemId());
2

As I understand it, the content delivery API isn't intended to support your use-case. Rather, it's supposed to let you link to the best place, without worrying about how it's done.

The best place to determine which pages a component is on is the Content Manager. There you have very good support for Where-Used queries.

If there's a particular reason why you need to do this on Content Delivery, perhaps you should share that with us, but otherwise, I'd say do it on the CM.

Your Answer

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

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