Copied to clipboard

Flag this post as spam?

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


  • BJ Patel 80 posts 206 karma points
    Apr 23, 2019 @ 14:34
    BJ Patel
    0

    Create new content nodes programmatically in Umbraco 8.

    Hello,

    In Umbraco 7 I used the following code to generate code programmatically from C# (controller)

    using ContentService.CreateContent
    

    And following is the code for the same

           int parentID = 1100;
    
            var request = ContentService.CreateContent("New Node Name", parentID, ContactUsForm.ModelTypeAlias);
    
            request.SetValue(ContactRequestItem.GetModelPropertyType(C => C.FirstName).PropertyTypeAlias, FormModel.FirstName);
    
            ContentService.PublishWithStatus(request);
    

    Now in Umbraco 8

    it is asking for

    Udi ParentId

    getting error "Can not convert 'int' to 'Umbraco.Core.Uid' ".

    Have searched a lot, but can't find anything for Umbraco 8.

    So now the question is How we can create a node from a controller in Umbraco 8?

    Thanks BJ

  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Apr 23, 2019 @ 16:15
    Matt Barlow | jacker.io
    0

    You need to pass in the Guid of the parent node.

    var parentId = new Guid("9D2B0228-4D0D-4C23-8B49-01A698857709")
    

    To get the guid of node you can now access it via the "Key" property of your node.

    Or go into the backend and find it on the info tab:

    enter image description here

  • BJ Patel 80 posts 206 karma points
    Apr 23, 2019 @ 16:31
    BJ Patel
    0

    Thank Matt,

    I have tried passing the same but getting error "Can not convert from System.Guid" to Umbraco.Core.Udi"

    enter image description here

    Any suggestion?

    Thanks BJ

  • Matt Barlow | jacker.io 164 posts 740 karma points c-trib
    Apr 23, 2019 @ 19:31
    Matt Barlow | jacker.io
    102

    Your intellisense is picking up the wrong method signature. Therefore you are referencing an incorrect library.

    using System;
    using System.Web.Mvc;
    using Umbraco.Core.Services;
    using Umbraco.Web.Models;
    
    public class TestController : Umbraco.Web.Mvc.RenderMvcController
    {
        public IContentService _contentService { get; set; }
    
        public TestController(IContentService contentService)
        {
            _contentService = contentService;
        }
    
    
        public override ActionResult Index(ContentModel model)
        {
            var parentId = new Guid("3cce2545-e3ac-44ec-bf55-a52cc5965db3");
            var request = _contentService.Create("test", parentId, ContentPage.ModelTypeAlias);
            _contentService.SaveAndPublish(request);
            return View();
        }
    }
    

    I think your using is incorrect and you are referencing Umbraco 7 libraries?

  • iNETZO 133 posts 496 karma points c-trib
    Oct 07, 2019 @ 09:38
    iNETZO
    0

    I have the problem. Did you solve this BJ Patel?

  • BJ Patel 80 posts 206 karma points
    Oct 07, 2019 @ 10:03
    BJ Patel
    0

    Hi iNETZO,

    Yes, Following is my code. maybe it will help you.

      public class ___Controller : Umbraco.Web.Mvc.SurfaceController
        {
        public IContentService _contentService { get; set; }
    
        public ___Controller (IContentService contentService)
                {
                    _contentService = contentService;
                }
       void CreateUmbracoNode()
    {
         var ContentService = Services.ContentService;
    
    var request = _contentService.Create("strNodeName", ParentNode.Key, FrmRequest.ModelTypeAlias);
    
    request.SetValue(FrmRequest.GetModelPropertyType(C => C.UserName).Alias, "Value ");
    
    _contentService.SaveAndPublish(request);
    
    }
    
        }
    

    Hope it may be helpful to you.

    Thanks BJ Patel.

  • Muneer 5 posts 75 karma points
    Nov 27, 2019 @ 07:54
    Muneer
    0

    Hello,

    Can we Create Language variants nodes programmatically in Umbraco 8 ?

  • Hassan Mourad 5 posts 75 karma points
    May 12, 2020 @ 09:30
    Hassan Mourad
    0

    have you found out how to Create Language variants nodes programmatically in Umbraco 8 ????????????????

  • Daniel B 7 posts 77 karma points
    May 28, 2020 @ 14:30
    Daniel B
    0

    Hi Hassan;

    Im new with umbraco 8; but i found a post with info related to 'culture' properties that could be useful:

    Content Variant APIs examples:

    var c2 = new Content("hello world", myContentType, "en-US");

    //this will set the Spanish name

    c2.SetCultureName("hola", "es-ES");

    var result = ContentService.SaveAndPublish(c2); //will publish all cultures

    source: https://github.com/umbraco/Umbraco-CMS/issues/4452#issuecomment-462207275

Please Sign in or register to post replies

Write your reply to:

Draft