Copied to clipboard

Flag this post as spam?

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


  • Sean Davis 13 posts 83 karma points
    Aug 23, 2021 @ 13:40
    Sean Davis
    0

    Creating content node programmatically doesnt show up on website until publish with descendants

    I have some code that creates content when a certain doc type is saved for the first time:

    private void ContentService_Saved_AddChildrenForStore(IContentService sender, ContentSavedEventArgs e)
        {
            foreach (var node in e.SavedEntities)
            {
                var dirty = (IRememberBeingDirty)node;
                var isNew = dirty.WasPropertyDirty("Id");
    
                if (!isNew) { continue; }
                if (node.ContentType.Alias == Restaurant.ModelTypeAlias
                    || node.ContentType.Alias == ShoppingStore.ModelTypeAlias
                    || node.ContentType.Alias == ServicesStore.ModelTypeAlias
                    || node.ContentType.Alias == SpecialtyStore.ModelTypeAlias
                    )
                {
                    SaveEventActions.CreateSalesAndJobOpportunitiesChildren(_contentService, node);
                }
            }
        }
    
    public static void CreateSalesAndJobOpportunitiesChildren(IContentService contentService, IContent parent)
        {
            var sales = contentService.Create("Sales", parent.Key, Sales.ModelTypeAlias);
            var jobs = contentService.Create("Job Opportunities", parent.Key, JobOpportunities.ModelTypeAlias);
            contentService.SaveAndPublish(sales);
            contentService.SaveAndPublish(jobs);
        }
    

    And this is the code that retrieves the information I'm trying to display:

    public override ActionResult Index(ContentModel model)
        {
            var salesPage = model.Content as SalesPromotionsPage;
    
            salesPage.Sales = model.Content.Parent.DescendantsOfType(Sale.ModelTypeAlias).Select(x => x as Sale);                           
    
            return View(salesPage);
        }
    

    I can't get any content created from my first codeblock to show up until I select an ancestor and click Publish with descendants. Maybe this is a problem with the front end cache not updating but I'm not sure how to fix it.

    The content appears to save in the content tree correctly. I can add child items and everything.

    Any help here?

  • Sean Davis 13 posts 83 karma points
    Aug 23, 2021 @ 14:52
    Sean Davis
    0

    It looks like the problem comes from calling SaveAndPublish() on the child nodes, since this is in the saved event, publish has not been called yet on the parent.

Please Sign in or register to post replies

Write your reply to:

Draft