If I have a page called BluePage and it uses a component called BlueComponent, using core service, how would I find linked components that show up on the page but are not directly used by the page. For example, a PDF multimedia component linked in the BlueComponent. When the page renders, the PDF shows up. I need to write a report that will list any object used on a page whether it directly used by the page or as a linked component. Does anyone have any code samples they can share?
1 Answer
I think this code can help. This will give you the first level of Linked Components for existing component presentations in a page
IEnumerable<XElement> links = null;
XNamespace xLinkNS = XNamespace.Get("http://www.w3.org/1999/xlink");
CoreServiceClient channel = new CoreServiceClient();
PageData page = (PageData)channel.Read("[PageId]", new ReadOptions());
foreach (ComponentPresentationData cp in page.ComponentPresentations) {
ComponentData component = (ComponentData)channel.Read(cp.Component.IdRef, new ReadOptions());
if (!string.IsNullOrEmpty(component.Content)) {
XElement xContent = XElement.Parse(component.Content);
XNamespace xNS = XNamespace.Get(xContent.FirstAttribute.Value);
links = xContent.Descendants().Where(w => w.Attributes(xLinkNS + "href").Count() > 0);
}
foreach (XElement link in links) {
// Linked Components
}
}