This week's TBB is about resolving Component links statically - i.e. at publish time. This is considered terribly bad practice, but sometimes, in the field, it is just what the custmer needs. It really goes against every Tridion principle and it will make the website completely inconsistent (e.g. dead links cannot be prevented).
However, if you need to do it, this it how. The code below reads the 'Where Used' Pages for a Component. It takes the Page publish location URL and constructs a 'static' link to the Page. It will not even check if the page is published.
However, if you need to do it, this it how. The code below reads the 'Where Used' Pages for a Component. It takes the Page publish location URL and constructs a 'static' link to the Page. It will not even check if the page is published.
public string
ResolveLinkStatic(Engine engine, Package package, Component
component) {
if
(component.BinaryContent == null) { // is Content Component
XmlElement
node = component.GetListUsingItems(
new
UsingItemsFilter(engine.GetSession()) {
ItemTypes = new ItemType[] { ItemType.Page },
IncludedVersions = VersionCondition.OnlyLatestVersions
}
);
XmlNamespaceManager
nmgr = new XmlNamespaceManager(new NameTable());
nmgr.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");
XmlNodeList
nodeList = node.SelectNodes("/tcm:ListUsingItems/tcm:Item",
nmgr);
switch
(nodeList.Count) {
case
0:
log.Error("No page found for resolving Component link "
+ component.Id);
return
string.Empty;
case
1:
return
GetUrlForPage(engine, package, nodeList[0].SelectSingleNode("@ID").InnerText);
default:
log.Error("Several pages found for resolving Component link
" + component.Id + ". Using first
page");
return
GetUrlForPage(engine, package, nodeList[0].SelectSingleNode("@ID").InnerText);
}
} else { // is Multimedia Component
return
component.Id; // will be resolved later by Resolve
Links In Output TBB
}
}
private string
GetUrlForPage(Engine engine, Package package, string
pageTcmUri) {
Page
page = engine.GetObject(pageTcmUri) as Page;
return
page.PublishLocationUrl.Replace('\\', '/');
}
Comments