Copied to clipboard

Flag this post as spam?

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


  • Sonja 133 posts 621 karma points
    Aug 12, 2019 @ 11:37
    Sonja
    0

    Create content from C#

    Hi

    I have to insert some content from database using c#. I am reading the data and placing it into models. Lets say 1 model is

    public class Manufacturer
          {
                public int ManufacturerId { get; set; }
                public string ManufacturerName { get; set; }
         }
    

    I have same Document type and 1 root document type Manufacturers. that can have multiple content of Manufacturer. Now I have read a list of manufacturers and I need to create a content for all of them. I have

     foreach (var man in manufacturers)
     {
             InsertUmbracoManufacturer(man);
     }
    

    and if manufacturer with mamufacturerId(not same as UmbracoId) exists update in else insert new. So far I have:

    public void InsertUmbracoManufacturer(Manufacturer man)
            {
                //get the manufacturer folder 
                IContentService contentService = ApplicationContext.Current.Services.ContentService;
                var rootFolder =contentService.GetRootContent().Where(x => x.ContentType.Alias.Equals("Manufacturers")).First();
                if (rootFolder!=null)
                {
                    //get the node with manufacturerId =man.manufacturerId
                    var m=rootFolder.Children().Where(x=>x.ContentType.Alias.Equals("Manufacturer") && x.Children().Where(y=>y.Properties["manufacturerid"].Equals(man.ManufacturerId))  //not so sure please help
                    if (m!=null)
                    {
                        //if it does exist update it help here too
                    }
                    else
                    {
                        //if it doesn't exist insert help here too
                    }
                }
    

    Please help

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 12, 2019 @ 12:12
    Shaishav Karnani from digitallymedia.com
    100

    I have updated your snippet and have added code as required. Do let me know for any clarification?

    public void InsertUmbracoManufacturer(Manufacturer man) { //get the manufacturer folder IContentService cs = ApplicationContext.Current.Services.ContentService; var rootFolder =contentService.GetRootContent().Where(x => x.ContentType.Alias.Equals("Manufacturers")).First(); if (rootFolder!=null) { //get the node with manufacturerId =man.manufacturerId var m=rootFolder.Children().Where(x=>x.ContentType.Alias.Equals("Manufacturer") && x.Children().Where(y=>y.Properties["manufacturerid"].Equals(man.ManufacturerId)) //not so sure please help

    IContent saveNode = null; if (m!=null) { //if it does exist update it help here too saveNode = cs.GetById(m.Id); } else { //if it doesn't exist insert help here too saveNode = cs.CreateContent(man.ManufacturerName, rootFolder.Id, "propertyDetails", 0); } .... update all properties // propertyNode.SetValue("property", "value"); // cs.SaveAndPublishWithStatus(propertyNode);

            }
    
  • Sonja 133 posts 621 karma points
    Aug 14, 2019 @ 13:44
    Sonja
    0

    Thanks, it is something like that, just m is found more easily:

            IContentService contentService = ApplicationContext.Current.Services.ContentService;
            var rootFolder = contentService.GetRootContent().Where(x => x.ContentType.Alias.Equals("manufacturers")).First();
            if (rootFolder != null)
            {
                //get the node with manufacturerId =man.manufacturerId
                var m = rootFolder.Children().Where(x => x.Properties["manufacturerid"].Value.Equals(man.ManufacturerId.ToString())).FirstOrDefault();
                IContent saveNode = null;
                if (m != null)
                {
                    //if it does exist update it
                    saveNode = contentService.GetById(m.Id);
                    saveNode.SetValue("manufacturerName", man.ManufacturerName);
                    saveNode.Name = man.ManufacturerName;
                }
                else
                {
                    //if it doesn't exist insert
                    saveNode = contentService.CreateContent(man.ManufacturerName, rootFolder.Id, "Manufacturer");
                    saveNode.SetValue("manufacturerId", man.ManufacturerId);
                    saveNode.SetValue("manufacturerName", man.ManufacturerName);
    
    
                }
                contentService.SaveAndPublishWithStatus(saveNode);
            }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies