if (currentComponent.Schema.Title == "dtd_link_item")
{
var linkComponentFields = currentComponent.Fields();
var element = new XElement("SiteMapNode");
element.Add(new XAttribute("uri", currentComponent.Id));
element.Add(new XAttribute("url", currentComponentFields.AsText("linked_url")));
element.Add(new XAttribute("link", linkComponentFields.Component("linked_component").Id));
var title = linkComponentFields.AsText("title") != string.Empty ? linkComponentFields.AsText("title") : linkComponentFields.AsText("link_title");
title = title != string.Empty ? title : GetTitle(linkComponentFields);
element.Add(new XAttribute("title", title));
xRoot.Add(element);
}
This is how I am assigning it, but Template Builder gives the following error for the "link" assignment:
Unable to cast object of type 'Tridion.ContentManager.ContentManagement.Fields.SingleLineTextField' to type 'Tridion.ContentManager.ContentManagement.Fields.ComponentLinkField'.
at Tridion.ContentManager.ContentManagement.Fields.ItemFieldsExtensions.IfExists[FIELDTYPE,OUT](ItemFields fields, String fieldName, Func`2 f)
at ABC.NavigationXML.CreateProductsSolutionsXML.buildNavigation(XElement xRoot, Component component)
at ABC.NavigationXML.CreateProductsSolutionsXML.buildNavigation(XElement xRoot, Component component)
at ABC.NavigationXML.CreateProductsSolutionsXML.Transform(Engine engine, Package package)
at Tridion.ContentManager.Templating.Assembly.AssemblyMediator.Transform(Engine engine, Template template, Package package)
at Tridion.ContentManager.Templating.Assembly.CSharpSourceCodeMediator.RunTemplate(Engine engine, Package package, String templateUri, String className)
at Tridion.Templating.CSharpTemplate.CSharpSourceTemplate.Transform(Engine __engine, Package __package)
at Tridion.ContentManager.Templating.Assembly.CSharpSourceCodeMediator.Transform(Engine engine, Template template, Package package)
at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package)
linkComponentFields.Component("linked_component")
(solinked_component
) a ComponentLink field in the schema? The error suggests that this is a 'normal' text field...