Updating related nodes when the original is saved/published
I've got a client who wants to be able to copy a node, link it to the original, and then have the copied node update its information when the original has been modified. I've done custom code before that runs when a node is published/saved, but I'm not sure how to go about this.
How do you get a list of related nodes in the API? I'm also not sure of the best solution for iterating over the properties of any node type and copying over the updated fields.
This is some code I came up with after looking over the documentation, but I'm not sure if the GetRelations is getting the nodes related to that node or if it's getting just an id of a relation table entry:
using System.Linq; using umbraco.BusinessLogic; using umbraco.NodeFactory; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.web; using umbraco.cms.businesslogic.relation; namespace UmbracoAddons { public class CustomerUI : ApplicationBase { public static Document PreviousDocument; public CustomerUI() { Document.AfterSave += Document_AfterSave; } void Document_AfterSave(Document sender, SaveEventArgs e) { //get current node var modifiedNode = new Node(sender.Id); //load the relationship type var relType = RelationType.GetByAlias("relateDocumentOnCopy"); var relations = Relation.GetRelations(sender.Id, relType).ToList(); //loop through all related nodes foreach(var relation in relations) { var relatedNode = new Document(relation.Id); //set the value for each related node to equal the modified node foreach(Property prop in modifiedNode.Properties) { string name = prop.Alias; var value = prop.Value; relatedNode.getProperty(name).Value = value; } //publish the related node with the updated property values User author = User.GetUser(0); relatedNode.Publish(author); umbraco.library.UpdateDocumentCache(relatedNode.Id); } } } }
I'd really appreciate being pointed in the right direction!
The example is for multi-lingual sites but the concept is the same. The video doesn't show anything technical really, but the link to the source shows how the copying is handled. Check the TranslateHandler.cs file.
OK I have looked into this properly now and the code from the link I sent is pre-Umbraco 6.1.1, so it's obsolete. Umbraco 6.1.1 and above can use the RelationService along with the ContentService to get what you are after.
Updating related nodes when the original is saved/published
I've got a client who wants to be able to copy a node, link it to the original, and then have the copied node update its information when the original has been modified. I've done custom code before that runs when a node is published/saved, but I'm not sure how to go about this.
How do you get a list of related nodes in the API? I'm also not sure of the best solution for iterating over the properties of any node type and copying over the updated fields.
This is some code I came up with after looking over the documentation, but I'm not sure if the GetRelations is getting the nodes related to that node or if it's getting just an id of a relation table entry:
I'd really appreciate being pointed in the right direction!
Hi Eric,
I'm about to tackle something similar and found this, which appears to be very useful. http://umbraco.com/follow-us/blog-archive/2009/3/25/microsoft-translator-and-umbraco
The example is for multi-lingual sites but the concept is the same. The video doesn't show anything technical really, but the link to the source shows how the copying is handled. Check the TranslateHandler.cs file.
Cheers, Nathan
OK I have looked into this properly now and the code from the link I sent is pre-Umbraco 6.1.1, so it's obsolete. Umbraco 6.1.1 and above can use the RelationService along with the ContentService to get what you are after.
RelationService: http://our.umbraco.org/documentation/reference/Management-v6/Services/RelationService
ContentService: http://our.umbraco.org/documentation/reference/Management-v6/Services/ContentService
I hope this helps.
is working on a reply...