umbracoumbraco-contourumbraco8

Create new content nodes programmatically in Umbraco 8


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?


Solution

  • The solution is as suggested on the following the link

    on the Umbraco Forum

    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();
        }