如何以编程方式在Orchard中创建现有类型的内容项

时间:2015-10-09 09:12:52

标签: orchardcms orchardcms-1.9

我有一个通过Admin GUI创建的注册内容类型。它只有一个id和一个公共部分以及一些文本字段。我可以这样做:

var registration = services.ContentManager.New("Registration"); 
registration.Name = "My Name";
registration.Comment = "Some random comment";
services.ContentManager.Create(registration, VersionOptions.Published);

或者我是否必须经历在代码中完全定义我的内容类型并手动处理持久性的痛苦?我在网上找到的所有教程以及官方文档都只涉及从头开始创建内容类型和项目。似乎没有简单地创建现有类型的新项目的示例。

1 个答案:

答案 0 :(得分:2)

你可以这样做就好了。如果你想设置更多的设置,你应该把它做成动态,所以你可以简单地把任何值放在那里:

dynamic registration = services.ContentManager.New("Registration");

// It is dynamic, so you can now set any property on it:
// <contentItem>.<theContentPart>.<thePropertyName> = someValue
// <contentItem>.<theContentPart>.<theField>.Value = someValue

// Set properties of a part
registration.ThePartThatHasTheNameProperty.Name = "My Name";
registration.ThePartThatHasTheCommentProperty.Comment = "Some random comment";

// Set properties of a field
registration.ThePartThatHasTheField.TheField.Value = "Some value";

services.ContentManager.Create(registration, VersionOptions.Published);

或者,强烈打字:

var registration = services.ContentManager.New("Registration");

registration.As<SomePart>().SomeProperty = "Some value";
registration.As<SomeOtherPart>().SomeOtherProperty = "Some other value";