从.NET中的Embeddedschemafield错误中检索组件

时间:2012-07-02 08:04:15

标签: tridion

我一直致力于.NET C#TBB从模式中检索字段,而模式又会解析每个嵌入字段。我在模板中使用Razor Mediator来输出JSON。我是C#和.NET的新手,因此我正在使用和引用我的同事在其他模板中编写的代码.TBB的目标是在发布后检索其中一个嵌入字段中图像的确切URL ,并以纯文本形式将其推回包中。

这最终似乎有效,直到我将嵌入式模式的一个字段更改为多媒体链接。一旦我将Embeddedschemafields设置为循环,模板构建器就会使用o描述性错误

Operation is not supported on a new item or on a null URI.

我不知道为什么会这样,因为代码实际上工作。这就是为什么我认为更改其中一个嵌入式模式的字段可能是罪魁祸首,但是在查看此特定字段之前就会发生错误。我的代码在从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)
      {

有人有想法吗?

这是我从模板构建器获取的日志:

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)

1 个答案:

答案 0 :(得分:2)

我会做以下事情:

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);

    if (content != null && content.Contains(field.Name)) {
      EmbeddedSchemaField embeddedFields = (EmbeddedSchemaField)content[field.Name]; //boom

      MultimediaLinkField mmValue = null;

      ItemFields currentFields = null;

      foreach (ItemFields embeddedField in embeddedFields.Values)
      {

注意添加的检查是否已创建ItemFields对象,并且它包含您正在查找的字段。