Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
When i save and publish a document i want to be able to automatically add another document under there in code?
Does anyone know how to go around doing that?
Hi Nathan,
Umbraco events will be your best friend with this.
https://our.umbraco.com/documentation/reference/events/ContentService-Events
So what you need to do is create a published event and target the parent. The one you are saving and publishing.
When the event fires you can then insert the doc type in code as usual.
To create an event look at the documentation in the above link. To summarize though.
Here is a copy of one of my published events. It doesn't do exactly what you need but it's half the way there.
private void ContentService_Published(IContentService sender, ContentPublishedEventArgs args) { try { foreach (var node in args.PublishedEntities) { if (node.ContentType.Alias == "customRedirectsFolder") { CustomRedirectsFolder customRedirectsFolder = _siteHelper.GetPageById(node.Id) as CustomRedirectsFolder; if (customRedirectsFolder != null) { if(customRedirectsFolder.ImportCsvfile != null) { _siteService.ImportCustomRedirects(node.Id, customRedirectsFolder.SkipRecords, customRedirectsFolder.TakeRecords, customRedirectsFolder.ImportCsvfile.Url); node.Properties["importCSVFile"].SetValue(null); sender.SaveAndPublish(node); } else if (customRedirectsFolder.ReRunDataFix) { List<CustomRedirect> customRedirects = _siteHelper.GetCustomRedirectsNeedFixing(node.Id); if(customRedirects != null && customRedirects.Count() > 0) { int count = 1; foreach(CustomRedirect customRedirect in customRedirects) { _siteService.ImproveCustomRedirect(customRedirect); count += 1; } } node.Properties["reRunDataFix"].SetValue(false); sender.SaveAndPublish(node); } } } } } catch (Exception e) { _logger.Error<PublishedEvent>("ContentService_Published | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : ""); } }
Regards
David
Thanks Dave,
give I'll give that a try now.
Hey Dave,
I'm still having some issues with it but i can see how this would work once I've got my head around it.
Yeah its tricky to begin with but once you have done one the rest are easy.
I use events all the time for all sorts of stuff. They are very handy.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
Umbraco 8 Create content Programmatically after save and publish
When i save and publish a document i want to be able to automatically add another document under there in code?
Does anyone know how to go around doing that?
Hi Nathan,
Umbraco events will be your best friend with this.
https://our.umbraco.com/documentation/reference/events/ContentService-Events
So what you need to do is create a published event and target the parent. The one you are saving and publishing.
When the event fires you can then insert the doc type in code as usual.
To create an event look at the documentation in the above link. To summarize though.
Here is a copy of one of my published events. It doesn't do exactly what you need but it's half the way there.
Regards
David
Thanks Dave,
give I'll give that a try now.
Hey Dave,
I'm still having some issues with it but i can see how this would work once I've got my head around it.
Yeah its tricky to begin with but once you have done one the rest are easy.
I use events all the time for all sorts of stuff. They are very handy.
is working on a reply...