4
 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)
4
  • Is the field linkComponentFields.Component("linked_component") (so linked_component) a ComponentLink field in the schema? The error suggests that this is a 'normal' text field... Commented Jun 23, 2015 at 9:40
  • Hi! Welcome to Tridion Stack Exchange! Unfortunately, your first question is a bit difficult to answer. It's not really clear what API you are using or where the error occurs. It seems to me that this is all custom code - either written inside your company or it's from one of the open source projects available in the wild. As such, it's rather difficult to speculate on what the issue is. Could you clarify where the field extensions come from? And exactly which line throws the exception? Commented Jun 23, 2015 at 9:40
  • yes it is a component link field
    – user1576
    Commented Jun 23, 2015 at 9:53
  • I am getting a "Object reference not set error" if the "linked_component" field does not have a component attached. But, that is not the case if the "linked_url" field is left empty. I am not sure what I am missing here. Any help would be appreciated. Thanks in advance.
    – user1576
    Commented Jun 23, 2015 at 10:36

2 Answers 2

6

I'd cover all bases and check if the schema field exists (what if someone renames this later or you decide to allow another schema that only has some of the currently covered fields)...

linkComponentFields.Component("linked_component") 

exists and then ensure that

linkComponentFields.Component("linked_component").Id 

has a value.

var xmlLink = string.Empty;
if(linkComponentFields.Component("linked_component") != null) {
    xmlLink = (linkComponentFields.Component("linked_component").Id != string.Empty ? linkComponentFields.Component("linked_component").Id | xmlLink);
  }
6

As Peter mentioned in his comments that your question is unclear. But if I consider your last comment which says its a "Object Reference" error, So I think it would be best to check the Objects Variables if those are NULL (a normal C# programming practice) before using them.

so please check objects like currentComponent, linkComponentFields , linkComponentFields.Component for a null condition before using them.

For example:

if(linkComponentFields.Component("linked_component") != null)
{
// code here
}

Your Answer

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

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