Copied to clipboard

Flag this post as spam?

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


  • Kurt 29 posts 111 karma points
    Apr 29, 2016 @ 23:03
    Kurt
    0

    Deleting and Unpublishing related content nodes

    We have a multi-lingual site, where English is the "parent" and all others are copies of and related to the English parent. When we delete or unpublish a node in the English section, we'd like all the related nodes to also be affected.

    Documentation for these (particularly unpublish) has been very difficult to find, so I'm turning to the forums.

    I've tried the following, but nothing is happening when I do the actions.

    For delete I have:

    ContentService.Trashed += ContentService_Trashed;
    void ContentService_Trashed(IContentService sender, MoveEventArgs<IContent> e)
        {
            var rs = ApplicationContext.Current.Services.RelationService;
            var cs = ApplicationContext.Current.Services.ContentService;
    
            // get the parent
            foreach (var parentRelation in rs.GetByParentId(e.ParentId))
            {
                cs.MoveToRecycleBin(cs.GetById(parentRelation.ChildId));
            }
        }
    

    For the unpublish, I have (this is actually unpublishing the related node's parent and I'm not sure why):

    ContentService.UnPublished += ContentService_UnPublished;
    void ContentService_UnPublished(IPublishingStrategy sender, PublishEventArgs<IContent> e)
        {
            var rs = ApplicationContext.Current.Services.RelationService;
            var cs = ApplicationContext.Current.Services.ContentService;
    
            // go through each document unpublished
            foreach (var c in e.PublishedEntities)
            {                
                // get the parent
                foreach (var parentRelation in rs.GetByParentId(c.ParentId).Where(x => x.RelationType.Alias == RelationTypes.RelateDocumentOnCopy))
                {
                    // copy the item if not already a relation
                    cs.UnPublish(cs.GetById(parentRelation.ChildId));
                }
            }
        }
    
  • Justin 3 posts 75 karma points
    Dec 26, 2017 @ 22:03
    Justin
    0

    Did you ever get this figured out? I am trying to do something similar by automatically adding pages when a page is added in my English section. It is working, but I am getting multiple copies of pages added in some languages. I was wondering if you ever figured this out?

  • Justin 3 posts 75 karma points
    Dec 28, 2017 @ 19:46
    Justin
    2

    If you change your delete method to this it will work for you:

            ContentService.UnPublished += ContentService_UnPublished;
    
            void ContentService_Trashed(IContentService sender, MoveEventArgs<IContent> e)
            {
                var rs = ApplicationContext.Current.Services.RelationService;
                var cs = ApplicationContext.Current.Services.ContentService;
    
                // get the parent
                foreach (var parentRelation in rs.GetByParentId(e.MoveInfoCollection.Single().Entity.Id).Where(x => x.RelationType.Alias == RelationTypes.RelateDocumentOnCopy))
                {
                    cs.MoveToRecycleBin(cs.GetById(parentRelation.ChildId));
                }
    
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft