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 67 posts 172 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 136 posts 559 karma points
    Apr 23, 2019 @ 16:15
    Matt Barlow
    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 67 posts 172 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 136 posts 559 karma points
    Apr 23, 2019 @ 19:31
    Matt Barlow
    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 51 posts 226 karma points
    Oct 07, 2019 @ 09:38
    iNETZO
    0

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

  • BJ Patel 67 posts 172 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.

Please Sign in or register to post replies

Write your reply to:

Draft