I have a C# TBB which takes a component, and should be pushing a link into the package stack.
- If the component is a publication, and it references a multimedia component, the link should be a link to that multimedia component.
- If the component is a publication, and it links to an external URL, the link should be that external URL.
- Otherwise, the link is just a broker link to that component ID.
I tried instantiating a Tridion.ContentDelivery.Web.Linking.ComponentLink object, but I get "could not load file or assembly 'Tridion.ContentDelivery, Version=6.1.0.348, Culture=neutral, PublicKeyToken=ddfc895746e5ee6b' or one of its dependencies", so I'm assuming I don't have access to that DLL from templating.
What does the below code need to do to return a valid URL?
private string getcomponentlink(Component c)
{
string link = null;
if (c.Schema.Id.ItemId == PUBLICATION_SCHEMA_ITEM_ID)
{
string doclink = getlink(c, "DocumentLink");
string externallink = getlink(c, "ExternalDocumentLink");
if (!String.IsNullOrWhiteSpace(doclink))
{
// TODO: need to convert from multimedia component TCM ID to proper link
link = doclink;
}
else if (!String.IsNullOrWhiteSpace(externallink))
{
link = externallink;
}
}
else
{
// TODO: need to convert from component TCM ID to proper link
link = c.Id;
}
return link;
}
private string getlink(Component c, string field)
{
var node = c.Content[field];
var attrs = node != null ? node.Attributes : null;
var linkattr = attrs != null ? attrs["xlink:href"] : null;
string link = linkattr != null ? linkattr.Value : null;
return link;
}
Solution
Here's the now working code.
if (c.Schema.Id.ItemId == PUBLICATION_SCHEMA_ITEM_ID)
{
string docid = getlink(c, "DocumentLink");
string externallink = getlink(c, "ExternalDocumentLink");
if (!String.IsNullOrWhiteSpace(docid))
{
link = String.Format("href=\"{0}\"", docid);
}
else if (!String.IsNullOrWhiteSpace(externallink))
{
link = String.Format("href=\"{0}\"", externallink);
}
}
if (String.IsNullOrWhiteSpace(link)) {
link = String.Format("broker:href=\"{0}\"", c.Id);
}