Programmatically create childnode when creating parent node in Umbraco 7.1.x
Hi there,
I'm trying to figure out how to create an child node programmatically when I create a parent node. I know that I can use ContentService to create the child node like:
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
// get the Umbraco Content Service
var contentService = Services.ContentService;
var childnode = contentService.CreateContent(
name,
1000,
"myDocType",
0);
// if i would set some values
product.SetValue("price", myPrice);
// then save and publish it
contentService.SaveAndPublish(product);
The thing I don't know is how to activate this code when my parent node is saved/published in umbraco.
I had the time to test it and it works. Thanks for your time.
Just for reference here is what I did. In a class library I created the class below and added a reference to the library in my Umbraco website.
using Umbraco.Core; using Umbraco.Core.Events; using Umbraco.Core.Models; using Umbraco.Core.Publishing; using Umbraco.Core.Services;
namespace Your.Namespace { public class CreateSubDocument : ApplicationEventHandler { public CreateSubDocument() { ContentService.Published += Go; }
private void Go(IPublishingStrategy sender, PublishEventArgs args) { foreach (var node in args.PublishedEntities) { if (node.ContentType.Alias == "YourDocType") { // get the Umbraco Content Service var contentService = new ContentService(); var childnode = contentService.CreateContent( "Product", node.Id, "YourSubDocType", 0);
// if i would set some values childnode.SetValue("MetaTitle", "Product");
// then save and publish it contentService.SaveAndPublishWithStatus(childnode); } } } } }
As of 7.9.2, the snippet above doesn't work. I had to get the ContentService from a static instance of ApplicationContext instead of creating a new class from ContentService and change the cast type of PublishEventArgs. I managed to do this with the following code:
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Publishing;
using Umbraco.Core.Services;
namespace UmbracoMobil.Utils
{
public class CreateSubDocument : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += Go;
}
private void Go(IPublishingStrategy sender, PublishEventArgs<IContent> args)
{
foreach (var node in args.PublishedEntities)
{
if (node.ContentType.Alias == "YourNode")
{
// get the Umbraco Content Service
var contentService = ApplicationContext.Current.Services.ContentService;
var childnode = contentService.CreateContent("Child name", node.Id, "childDocAlias", 0);
// if i would set some values
//childnode.SetValue("MetaTitle", "Product");
// then save and publish it
contentService.SaveAndPublishWithStatus(childnode);
}
}
}
}
}
Programmatically create childnode when creating parent node in Umbraco 7.1.x
Hi there,
I'm trying to figure out how to create an child node programmatically when I create a parent node. I know that I can use ContentService to create the child node like:
The thing I don't know is how to activate this code when my parent node is saved/published in umbraco.
Can someone point me in the right direction?
/Martin
Hi Martin
I think you can benefit from the list of events found in the documentation here http://our.umbraco.org/documentation/Reference/Events-v6/ContentService-Events - Should be possible to hook into the save or saving events I think.
Don't worry to much about the reference points to something with "v6" in it. It's for v6+ - A little confusing, I know! :)
Hope this helps.
/Jan
Hi Jan,
That seems to be the right direction, I will test it tomorrow and give some feedback on it thanks ;o)
/Martin
Hi Jan,
I had the time to test it and it works. Thanks for your time.
Just for reference here is what I did. In a class library I created the class below and added a reference to the library in my Umbraco website.
Hi Martin
Happy to held and thank you for sharing so others can benefit, should they come across this post.
High 5 You Rock! :)
/Jan
As of 7.9.2, the snippet above doesn't work. I had to get the ContentService from a static instance of ApplicationContext instead of creating a new class from ContentService and change the cast type of PublishEventArgs. I managed to do this with the following code:
is working on a reply...