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
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.
How can I programmatically update these references to use relative path?
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.
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)
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:
Grab the new root node for which you need to update the references.
Iterate over the tree and for each node:
Get the full path
Replace the ancestors with the appropriate new ancestors
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; }
}
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
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.How can I programmatically update these references to use relative path?
Thanks
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, andSave
it.This is destined to break and hard to maintain, so I'm hoping someone knows a better way of doing it.
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!
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:
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
is working on a reply...