Copied to clipboard

Flag this post as spam?

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


  • Eric Patton 21 posts 51 karma points
    Jan 21, 2014 @ 17:18
    Eric Patton
    0

    Updating related nodes when the original is 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 MDESUmbracoAddons
    {
        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!

     

Please Sign in or register to post replies

Write your reply to:

Draft