Copied to clipboard

Flag this post as spam?

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


  • Fabian 68 posts 172 karma points
    Apr 26, 2017 @ 13:17
    Fabian
    0

    Saving a list of child nodes programmatically

    I have a case scenario where I have the need to save a list of about 100 or more child nodes. I am achieving this with the following code:

    foreach (var meal in meals)
                                    {
                                        var priceListMeal = Services.ContentService.CreateContent(
                                            meal.Name,
                                            priceList.Id,
                                            RomyFoodsConstants.PriceListMealDocumentTypeAlias);
    
                                        priceListMeal.SetValue(RomyFoodsConstants.PriceListMealLinkedMealPropertyAlias, meal.Id);
    
    
                                        listOfContents.Add(priceListMeal);
    
                                        var savePriceListMealAttempt = Services.ContentService.SaveAndPublishWithStatus(priceListMeal);
                                        if (!savePriceListMealAttempt.Success)
                                        {
                                            throw new Exception("Something went wrong while creating the price list meals.");
                                        }
                                    }
    

    It works just fine but the issue is that is not that fast. It takes a while for umbraco to save them. For this reason I was wondering if there is a better way to do this?

    Any help please?

    Please note that I had already done some research and found the below:

    But no one seems to have a solution and I thought maybe things have changed from when those question where posted.

    Thanks

  • Alan Thom 3 posts 76 karma points c-trib
    Apr 26, 2017 @ 14:53
    Alan Thom
    1

    Hi Fabian

    You should use the Save() method while iterating then call the PublishWithChildrenWithStatus() method on the parent node once you've added all your data. This ensures that the Umbraco cache is only rebuilt once, rather than after adding each item.

    You should end up with something similar to this:

    foreach (var meal in meals)
        {
            var priceListMeal = Services.ContentService.CreateContent(
                meal.Name,
                priceList.Id,
                RomyFoodsConstants.PriceListMealDocumentTypeAlias);
    
            priceListMeal.SetValue(RomyFoodsConstants.PriceListMealLinkedMealPropertyAlias, meal.Id);
    
    
            listOfContents.Add(priceListMeal);
    
            var savePriceListMealAttempt = Services.ContentService.Save(priceListMeal);
            if (!savePriceListMealAttempt.Success)
            {
                throw new Exception("Something went wrong while creating the price list meals.");
            }
        }
    
        Services.ContentService.PublishWithChildrenWithStatus(priceList);
    
Please Sign in or register to post replies

Write your reply to:

Draft