Copied to clipboard

Flag this post as spam?

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


  • Christian 5 posts 96 karma points
    Dec 10, 2017 @ 23:58
    Christian
    0

    Copying a root node and updating the references in all child nodes and content pckers

    Friends,

    We have several sites as root nodes, e.g. site1.mysite.com, site2.mysite.com in the same Umbraco instance. I have a problem where upon copying a root node, I want to update all child nodes, as well as all nodes in multi node content pickers, so that they reference the nodes in the newly created sites.

    Here's our tree structure tree structure

    So we Copy the Foundation Small template site, which has content pickers and media pickers and the lot, however in the newly created site, the multi node content pickers point to the template site. enter image description here

    How can I programmatically update these references to use relative path?

    Thanks

  • Christian 5 posts 96 karma points
    Dec 12, 2017 @ 02:59
    Christian
    100

    I've sort of solved this, but it's not an ideal way to do it.

    Starting at the root node of the new site tree, I traverse the tree depth-first and look for content pickers. I then manually generate a new string of UDIs for the content picker node, call SetValue on the node, and Save it.

    This is destined to break and hard to maintain, so I'm hoping someone knows a better way of doing it.

  • Bill 43 posts 65 karma points
    Mar 08, 2018 @ 11:44
    Bill
    0

    Hi Christian,

    I've run into this very issue and I was hoping you could give some advice please.

    Added wrinkle in that the site I'm looking at uses Nested Content quite heavily so I think I'd need to unpack the json from these fields, update the uid reference from inside the json manually and resave (this terrifies me)

    Thanks so much for any help you can offer!

  • Christian 5 posts 96 karma points
    Mar 08, 2018 @ 22:36
    Christian
    0

    Hi Bill,

    Sorry I've never used Nested Content so I have no idea how that works.

    If you can get nested content as IContent nodes you can do this the same way I did using the API.

    Edit: I just had a look at it. It's very similar to our homemade solution using the multinode tree pickers.

    You should be able to do this using the following steps:

    1. Grab the new root node for which you need to update the references.
    2. Iterate over the tree and for each node:
      1. Get the full path
      2. Replace the ancestors with the appropriate new ancestors
      3. Save changes to node

    I found this in the documentation so that should get you all the IContent nodes you need.

    I iterated the content tree using a simple graph structure so that I didn't end up doing the same nodes over and over if they are referenced in many nodes. e.g

    public class GraphNode<T>
    {
        public GraphNode(T data)
        {
            Data = data;
            Visited = false;
        }
        public T Data { get; set; }
    
        public bool Visited { get; set; }
    
        public string Path { get; set; }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft