2012-05-07 30 views
0

我正在处理用于保存组件的事件处理程序。在SDL中的事件处理函数中使用TOM.NET API获取重复嵌入模式的值Tridion 2011 SP1

我的目标是在用户根据模式创建组件时执行一些验证。

我有一个名为“Employee”的模式。

员工有一个名为“Experience”的嵌入式架构,它是多值的。

经验有3个字段。

  1. 作用:用值Manager,Lead下拉菜单。
  2. 公司:文本字段
  3. 年:文本字段

当用户进入这些领域的一些数据,我希望做一些验证之前保存。

高层次的设计看起来像这样。

  1. 加载该组件
  2. 导航到嵌入式领域的 “经验”

对于每一个 “经验” 的实例。我需要的“角色”的值,并检查相应的值在其他两个字段中输入(通过编写组件保存事件)

For(all the repeated "Experience") 
{ 
    If (Role=="Manager") 
     check the values in the other two fields and do some validation 
    If (Role=="Lead") 
     check the values in the other two fields and do some validation 
} 

我停留在在提取子字段的值和名称嵌入式领域。

我曾尝试:

Tridion.ContentManager.Session mySession = sourcecomp.Session; 
Schema schema= sourcecomp.Schema; 
if(schema.Title.Equals("Employee")) 
{ 
    var compFields = new ItemFields(sourcecomp.Content, sourcecomp.Schema); 
    var embeddefield = (EmbeddedSchemaField)compFields["Experience"]; 

    var embeddedfields = (IList<EmbeddedSchemaField>)embeddefield.Values; 
    foreach(var a in embeddedfields) 
    { 
     if(a.Name.Equals("Role")) 
     { 
      string value=a.Value.ToString(); 
     } 
    } 
} 

其实我卡住了如何在同一时间检索其他字段中的值。

任何人都可以解释它是如何完成的吗?

+0

您可以指定您正在使用的SDL Tridion的哪个版本? –

+0

我正在使用SDL Tridion 2011 SP1。 – Patan

回答

4

你需要了解在EmbeddedSchemaField类是它代表架构和字段(顾名思义...)

我总是发现它有助于看看源XML该组件在编写针对其字段的代码时,可以很好地直观地表示您的类必须执行的操作。如果你看一个组件XML这样的:

<Content> 
    <Title>Some Title</Title> 
    <Body> 
      <ParagraphTitle>Title 1</ParagraphTitle>   
      <ParagraphContent>Some Content</ParagraphContent> 
    </Body> 
    <Body> 
      <ParagraphTitle>Title 2</ParagraphTitle>   
      <ParagraphContent>Some more Content</ParagraphContent> 
    </Body> 
</Content>   

身体是你的嵌入式架构领域,这是多值,并包含在其中2单值的字段。

解决TOM.NET这些领域,那么:

// The Component 
Component c = (Component)engine.GetObject(package.GetByName(Package.ComponentName)); 
// The collection of fields in this component 
ItemFields content = new ItemFields(c.Content, c.Schema); 
// The Title field: 
TextField contentTitle = (TextField)content["Title"]; 
// contentTitle.Value = "Some Title" 
// Get the Embedded Schema Field "Body" 
EmbeddedSchemaField body = (EmbeddedSchemaField)content["Body"]; 
// body.Value is NOT a field, it's a collection of fields. 
// Since this happens to be a multi-valued field, we'll use body.Values 
foreach(ItemFields bodyFields in body.Values) 
{ 
    SingleLineTextField bodyParagraphTitle = (SingleLineTextField)bodyFields["ParagraphTitle"]; 
    XhtmlField bodyParagraphContent = (XhtmlField) bodyFields["ParagraphContent"]; 
} 

希望这可以让你开始。

+0

还值得一提的是,sourcecomp.Content包含组件XML的XML,因此您可以简单地从XML解析所需的信息。 – johnwinter

+0

感谢您的信息。 – Patan

相关问题