Martin Haščák
Martin Haščák

Reputation: 350

Orchard - Insert content item to Content Type programmatically

I have problem to insert content item to Content type. In Orchard administration I created custom content type with fields: name, surname, date etc...

But inserting new content items I must do in controller method programmatically. I tried this, buth this no work:

var item = this._services.ContentManager.New("Zadosti");
item.Name.Value = "Some dummy usage of this product"; // I can not access name field
this._services.ContentManager.Create(item);

Upvotes: 0

Views: 527

Answers (1)

devqon
devqon

Reputation: 13997

You probably attached the fields directly to the custom "Zadosti" content type? What Orchard in that case does is attaching the fields to a part named exactly the same as the type, it doesnt ever attach the fields to the content type itself (dont let the dashboard fool you!)

Therefore you can access the field as following:

var item = this._services.ContentManager.New("Zadosti"); 

// 'Zadosti' in here is the name of your part, which is 
// the same as your content type name
item.Zadosti.Name.Value = "Some dummy usage of this product"; 
this._services.ContentManager.Create(item);

Upvotes: 3

Related Questions