Document_AfterNew - how to automatically create child nodes?
Hi
I need to automatically create a couple of child nodes for a specific document type when a user creates it in the umbraco content tree. I have been trying to do it like this but am having no success:
The event is raised ok but the Document I'm trying to parse out of the sender is coming back empty - the UniqueId is: 00000000-0000-0000-0000-000000000000 and doc.ContentType is null. The following like returns a Document but it is as though it has not yet been created.
Document doc = new Document(true,((CMSNode)sender).Id);
@Zakhar This looks like exactly what I am looking for. Where does this class go? Is this a complied dll added in or is possible to do something like this from inside of the Umbraco CMS?
Document_AfterNew - how to automatically create child nodes?
Hi
I need to automatically create a couple of child nodes for a specific document type when a user creates it in the umbraco content tree. I have been trying to do it like this but am having no success:
Document.AfterNew += Document_AfterNew;
...
void Document_AfterNew(object sender, NewEventArgs e) {
Document doc = new Document(true,((CMSNode)sender).Id);
if (doc.ContentType.Alias == "myDocType") {
Document.MakeNew("Page Name", DocumentType.GetByAlias("AnotherDocType"), umbraco.BusinessLogic.User.GetCurrent(), doc.Id);
}
}
The event is raised ok but the Document I'm trying to parse out of the sender is coming back empty - the UniqueId is: 00000000-0000-0000-0000-000000000000 and doc.ContentType is null. The following like returns a Document but it is as though it has not yet been created.
Document doc = new Document(true,((CMSNode)sender).Id);
Any thoughts on how to accomplish this?
Thanks
Hi, try this:
public class MyDocTypeCreation : ApplicationBase {
public MyDocTypeCreation() {
Document.New += CreateSubNodes;
}
private static void CreateSubNodes(Document sender, umbraco.cms.businesslogic.NewEventArgs e) {
if (sender.ContentType.Alias == "MyDocType") {
if (!sender.HasChildren) {
string name = sender.Text;
User currentUser = User.GetCurrent();
Log.Add(LogTypes.Custom, sender.Id, "Creating child node for" + name);
Document doc = Document.MakeNew("Child name",
DocumentType.GetByAlias("ChildType"),
currentUser,
sender.Id);
doc.getProperty("myProp").Value = 0;
doc.Save();
// optional
//doc.Publish(currentUser);
//library.UpdateDocumentCache(doc.Id);
}
}
}
Wonderful - works perfectly - thank you very much!
@Zakhar This looks like exactly what I am looking for. Where does this class go? Is this a complied dll added in or is possible to do something like this from inside of the Umbraco CMS?
is working on a reply...