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:
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);
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:
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
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:
is working on a reply...