Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nathan Reece 62 posts 376 karma points
    Aug 18, 2020 @ 09:01
    Nathan Reece
    0

    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?

  • David Armitage 505 posts 2073 karma points
    Aug 18, 2020 @ 09:05
    David Armitage
    100

    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.

    1. Create a component for your published event
    2. Then create a composer to tell Umbraco about your event.

    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

  • Nathan Reece 62 posts 376 karma points
    Aug 18, 2020 @ 09:13
    Nathan Reece
    0

    Thanks Dave,

    give I'll give that a try now.

  • Nathan Reece 62 posts 376 karma points
    Aug 18, 2020 @ 09:31
    Nathan Reece
    0

    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.

  • David Armitage 505 posts 2073 karma points
    Aug 18, 2020 @ 09:33
    David Armitage
    1

    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.

Please Sign in or register to post replies

Write your reply to:

Draft