I’ve been working on a .NET C# TBB to retrieve fields from a schema, which in turns parses through every embedded field. I use Razor Mediator in my templates to output JSON. I’m very new to C# and .NET and I’m therefore using and referring code my colleagues wrote in other templates.The goal of the TBB is to retrieve, after publish, the exact url of an image within one of these embedded fields, and push it back to the package as plain text.
This eventually seemed to work, up until I changed one of the fields of the embedded schema’s to a multimedialink. As soon as I set the Embeddedschemafields to loop through, template builder serves an error with the o so descriptive
Operation is not supported on a new item or on a null URI.
I have no clue why this is happening, since the code actually worked . That’s why I believe changing one of the embedded schema’s fields could be the culprit, but the error occurs even before looking this particular field up. my code stops at retrieving the component from the embeddedschemafield:
CM.Schema schema = (engine.GetObject(comp.GetAsSource().GetValue("ID")) as CM.Component).Schema;
ItemFields fields = new ItemFields(schema);
foreach (ItemField field in fields)
{
_log.Info("top level: " + field.Name);
if (field is EmbeddedSchemaField)
{
CM.Component c = (CM.Component)engine.GetObject(package.GetByName(Package.ComponentName));
_log.Info(" c = " + c + " content: " + c.Content + " schema: " + c.Schema);
ItemFields content = new ItemFields(c.Content, c.Schema);
EmbeddedSchemaField embeddedFields = (EmbeddedSchemaField)content[field.Name]; //boom
MultimediaLinkField mmValue = null;
ItemFields currentFields = null;
foreach (ItemFields embeddedField in embeddedFields.Values)
{
Anyone have an idea?
This is the log I got from template builder:
GetComponentImageUrl: top level: name
GetComponentImageUrl: top level: division
GetComponentImageUrl: top level: theme
GetComponentImageUrl: top level: product
GetComponentImageUrl: field.Name = Embeddedschemafield
GetComponentImageUrl: c = Component tcm:11-1963 content: System.Xml.XmlElement schema: Schema tcm:11-1842-8
GetComponentImageUrl: c. Content = System.Xml.XmlElement
Engine: Error in Engine.Transform
Engine: Error in Engine.Transform
Operation is not supported on a new item or on a null URI.
at Tridion.ContentManager.Session.GetTcmUri(String uri)
at Tridion.ContentManager.Session.GetObject(String uri)
at Tridion.ContentManager.Session.GetObject(XmlElement linkElement)
I would do the following:
Notice the added check that the ItemFields object is created, and that it contains the field you’re looking for.