How to create Item/Page/Subpage using code and not from Edit mode

Topics: Developer Forum, Project Management Forum, User Forum
Feb 28, 2009 at 8:25 AM

I am new with N2CMS. We can easliy create/edit item/page/subpage from edit mode. Is it possible to create/modify the existing item/page/subpage without edit mode and from the public site itself on some action of user?

Sunil Gami
Feb 28, 2009 at 10:55 AM
When i need to ensure that a certain MyItem always contain a child of type MySubItem, i use to arrange it this way:

using N2;

class MyItem
public MySubItem SubItem {
get { return this.GetOrFindOrCreateChild<MySubItem>("childName", _newChild => _new.Child.Title = "Child Title"; }); }

Now, the first time you access that property, be it from the Edit GUI or the site itself, an appropriate child will be created and persisted, so that on the next access it will be retrieved much quicker.

If you follow the link, you'll hopefully get how that GetOrFindOrCreateChild extension method was implemented. For me all of this is a heavily used pattern, so i've put it into the library.