Copied to clipboard

Flag this post as spam?

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


  • Gean Leonor 3 posts 73 karma points
    1 week ago
    Gean Leonor
    0

    Create and update created Child item Programmatically.

    Hi,

    I'm new with the Umbraco I'm trying to import all the product item to Umbraco Child item and was able to create by the help of the link below. However, I was not able to query the created item to update the value. Can anyone help me out on how?

    https://our.umbraco.com/documentation/Reference/Management/Services/ContentService/Create-content-programmatically

    ~Kyle

  • Brendan Rice 408 posts 654 karma points
    1 week ago
    Brendan Rice
    0

    Hi Gean,

    can you share your code that you are trying to do the query with?

  • Gean Leonor 3 posts 73 karma points
    1 week ago
    Gean Leonor
    0

    Hi Brendan,

    please see below for the sample code. what we want to achieve is to able to search the created item so we could update it on daily process.

    Thank you for your quick response.

    using Essential.Umbraco.Core.Models;
    using Essential.Umbraco.Service.Data;
    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Threading.Tasks;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Services;
    
    namespace Essential.Umbraco.Service.Services
    {
        public class WebCatService : IDisposable
        {
            private readonly IContentService _contentService;
    
            private readonly Guid PRODUCT_PAREND_ID = Guid.Parse("6d302ab9-b948-4bbd-a067-7ff568d57184");
            public WebCatService()
            {
                _contentService = Current.Services.ContentService;
            }
    
            public async Task ProcessProductList()
            {
                using (var db = new EssentialDBContext())
                {
                    var webcats = await db.WebCat
                        .Select(s => new WebCatDTO
                        {
                            Barcode = s.Barcode,
                            catname = s.catname,
                            Code = s.Code,         
                            Description = s.Description,
                            Kilo = s.Kilo,
                            Pack_Size = s.Pack_Size,
                            Price = s.Price,
                        })
                        .ToListAsync();
    
                    foreach (var item in webcats)
                    {
                        if (!UpdateIfExist(item))
                            Create(item);
                    }
                }
            }
    
            public void Create(WebCatDTO item)
            {
                var contentService = _contentService;
    
                var productItem = contentService.Create($"{item.catname} - {item.Code}", PRODUCT_PAREND_ID, "product");
    
                productItem.SetValue("webCatname", item.catname);
                productItem.SetValue("code", item.Code);
                productItem.SetValue("description", item.Description);
                productItem.SetValue("packSize", item.Pack_Size);
                productItem.SetValue("price", item.Price);
                productItem.SetValue("Kilo", item.Kilo);
                productItem.SetValue("barcode", item.Barcode);
    
                contentService.SaveAndPublish(productItem);
            }
    
            public bool UpdateIfExist(WebCatDTO item)
            {
                var exist = false;
                //
                // Query to check if the product exists by item.code
                //
    
                if (exist) return false;
    
                //
                // Code to update the item if exist
                //
    
                return false;
            }
    
            public void Dispose()
            {
                GC.Collect();
            }
        }
    }
    

    ~ Kyle

  • Alex Skrypnyk 5978 posts 22689 karma points MVP 4x admin c-trib
    1 week ago
    Alex Skrypnyk
    1

    Hi Gean,

    Generally speaking, this code looks like not the best architecture.

    But you can get node by property value with this code:

    var existingItem = Umbraco.AssignedContentItem.Root().Descendants().FirstOrDefault(x => x.HasValue("code") && x.Value("code") == item.Code);
    

    It's not the best in terms of performance; you probably have to use Examine index for retrieving existing items or some caching.

    Thanks,

    Alex

  • Gean Leonor 3 posts 73 karma points
    1 week ago
    Gean Leonor
    0

    Hi Alex,

    Thank you so much for your advice. we did a different approach. we just stored the created IContent id somewhere so that when we search the item we can use the _ContentService.GetId(). And the code you provide we used it when the result from _ContentService.GetId() is equal null.

    Thank again. the code is really helpful.

    ~Gean Kyle

  • Alex Skrypnyk 5978 posts 22689 karma points MVP 4x admin c-trib
    1 week ago
    Alex Skrypnyk
    0

    Hi

    You choose a better way, cool, thanks for sharing.

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft