Copied to clipboard

Flag this post as spam?

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


  • Javz 38 posts 141 karma points
    May 04, 2016 @ 11:29
    Javz
    0

    Copying content but preserving published/unpublished status

    Hi all,

    I've been wanting to sign-up since 2014, but tend to find my answers or fix the issue. Now I finally have :)

    Anyways, I'm trying to duplicate a node and all its descendants within the backoffice and preserving the published/unpublished status. Essentially copying it as is, but obviously the name would be renamed by default which I'm fine with.

    Is there a way of doing this? I've been looking for a while and have had no answers. I would have assumed this would have been a default option within Umbraco, or perhaps I've missed something completely?

    Many thanks!

  • Javz 38 posts 141 karma points
    May 05, 2016 @ 13:54
    Javz
    0

    Any ideas anyone?

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 05, 2016 @ 14:15
    Dennis Adolfi
    0

    Hi Javz!

    No, copied nodes in Umbraco always gets Unpublished, even if the original node was published, this is the default behavior. Would be nice to have it as an option, maybe you can submit it as a feature request to http://issues.umbraco.org? Or better yet, build your own package that enabled this option?

    Here is a code-snippet for a applicaion-startup class that will do what you are after. Add this class to your solution

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace MyApplication.App_Code
    {
        public class ApplicationStartup : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Copied += ContentServiceOnCopied;
            }
    
            private void ContentServiceOnCopied(IContentService sender, CopyEventArgs<IContent> e)
            {
                if (e.Original.Published && e.Copy.Published == false)
                {
                    ApplicationContext.Current.Services.ContentService.Publish(e.Copy);
                }
            }
        }
    }
    

    Note, this only publishes the first level node, you´ll have to tweak it a little to get it to go through all the decendants, but this is just to get you started.

    Best of luck to you, let me know how it works out!

    All the best! / Dennis

  • Javz 38 posts 141 karma points
    May 05, 2016 @ 15:49
    Javz
    0

    Hi Dennis!

    Thanks for that! It's brilliant!

    From the given logic, I'm trying to get the children of "e" and find whether that is published or not? Or do I do a foreach statement then doing a comparison?

    Though may I ask how to do the lower levels? Apologies, I'm not entirely familiar with customising an ApplicationBase.

    Thanks for your time and effort; much appreciated :D

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 05, 2016 @ 16:30
    Dennis Adolfi
    0

    Yes I would do a foreach statement and probably traverse the children and its children and do a compasion, I'm not sure completey, answering from my phone at the moment.

    Please let me know how it goes for you. If it's not fixed by tomorrow I can have a look at it when I'm back at work.

    Best of luck!!

  • Javz 38 posts 141 karma points
    May 05, 2016 @ 16:43
    Javz
    1

    I added the following first and it gave me an error when checking published statuses, but copy's over unpublished nodes:

            if (e.Original.Children().FirstOrDefault().Published && e.Copy.Children().FirstOrDefault().Published == false)
            {
                ApplicationContext.Current.Services.ContentService.Publish(e.Copy);
            }
    

    I had then tried doing the foreach, but seem to get an error as "IContent does not contain a publish definition for 'GetEnumerator'"

    All of this was done inside of the "ContentServiceOnCopied" method.

    Thanks again Dennis!!

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 05, 2016 @ 16:38
    Dennis Adolfi
    0

    Yes I would do a foreach statement and probably traverse the children and its children and do a comparsion, I'm not sure completey, answering from my phone at the moment.

    Please let me know how it goes for you. If it's not fixed by tomorrow I can have a look at it when I'm back at work.

    Best of luck!!

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 05, 2016 @ 21:07
    Dennis Adolfi
    0

    I'll get back to you tomorrow with a solution as soon as I'm back at work. :) All the best!!

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 06, 2016 @ 06:47
    Dennis Adolfi
    100

    Hi Javz.

    So, here is a updated version that also preserves the publish/unpublish status for the copied node and all its descendants.

    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace MyApplication.App_Code
    {
        public class ApplicationStartup : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                ContentService.Copying += ContentServiceOnCopied;
            }
    
            private void ContentServiceOnCopied(IContentService sender, CopyEventArgs<IContent> e)
            {
                if (e.Original.Published && e.Copy.Published == false)
                {
                    e.Copy.ChangePublishedState(PublishedState.Published);
                    ApplicationContext.Current.Services.ContentService.SaveAndPublishWithStatus(e.Copy);
                }
            }
        }
    }
    

    See animation for result:

    enter image description here

    Let me know if it works out for you!! :)

  • Javz 38 posts 141 karma points
    May 06, 2016 @ 08:36
    Javz
    0

    Hey Dennis! Thanks for that!!

    Unfortunately, I don't get the exact outcome; when copying I get the following error

    "Received an error from the server Failed to copy content

    The query didn't return a value.

    EXCEPTION DETAILS:

    System.ArgumentException: The query didn't return a value. STACKTRACE:

    at umbraco.DataLayer.SqlHelper1.ExecuteXmlReader(String commandText, P[] parameters) at umbraco.DataLayer.SqlHelper1.ExecuteXmlReader(String commandText, IParameter[] parameters)"

    But once I refresh the page, the content has somewhat copied successfully, as it preserves the published/unpublished status, but in a different order than the original.

    I'm using Umbraco 7.2.7, MSSQL, IIS. Also I am not checking the box "Relate to original" either (but I have done it and receive the same result)

    Also could explain the process? The logic I thought of yesterday doesn't apply in this instance.

    Thanks again :)

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 06, 2016 @ 09:18
    Dennis Adolfi
    0

    Hi Javz.

    Unfortunatly, i can not re-produce this error. I installed a Umbraco 7.2.7 installation with a MsSql server, not ticked the "Relate to original" but this code workes fine for me.

    Could you maybe open the solution in Visual Studio so that you can debug with breakpoints?

    Sorry i could´nt be of anymore help..

  • Javz 38 posts 141 karma points
    May 06, 2016 @ 09:27
    Javz
    0

    Hi Dennis,

    I will after this current test I am doing.

    Initially I copied with small amounts of data, and received the error; so I wasn't sure if I received the error before the copy or after the copy had been completed.

    Now I'm copying a large amounts of data, and can see that it is still copying; I assume it only throws the error once the copying has been completed.

    Will try to debug after this copy has completed and let you know my findings!

    Thanks :D

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 06, 2016 @ 09:37
    Dennis Adolfi
    0

    I see, yes it would be interesting to find out why this works for me and not for you, and i think that you will see the reason for that if you are debugging your code.

    Also, it could be interesting if you tried this with a clean installation of Umbraco on the same version. That way you could circle wheater or not it might be something wrong with your content, or if the code is incorrect.

    Also, off course, bumping the ClientDependency.config version number never hurt. :)

    Im all out of ideas unfortunatly. :(

  • Javz 38 posts 141 karma points
    May 06, 2016 @ 09:48
    Javz
    1

    I also thought it might have been content related but it wasn't.

    Apologies Dennis, it was my fault - I forgot to update "Copied" to "Copying", it is working now :)

    For testing, I've also changed your original solution to copying as well, and that also works.

    I'm assuming the difference between the two solutions, besides the "copying" is that one is publishing whilst the other is saving and publishing?

    Thanks your help again, very much appreciated!

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 06, 2016 @ 09:54
    Dennis Adolfi
    1

    Yes, that is the differece between the two. But the first solution did´nt publish the children for some reason. The second solution does however.

    So everthing is working for you now? Awesome, that makes me very happy!! Now i can move on and not think about this anymore. :)

    Have a great day, and good luck with the rest of your Umbraco install!

  • Javz 38 posts 141 karma points
    May 06, 2016 @ 09:57
    Javz
    0

    You've made my day Dennis! Thanks again and I have marked the latter one as the appropriate solution :)

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    May 06, 2016 @ 09:58
    Dennis Adolfi
    0

    Awsome!! Thank you! Glad i could help!! :)

Please Sign in or register to post replies

Write your reply to:

Draft