0

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?

BJ Patel
  • 6,148
  • 11
  • 47
  • 81

3 Answers3

0

How about getting the parent node first (this can be done via int ID) and then get the UDI from that? Something like

var parent = ContentService.GetById(1100);
var request = ContentService.CreateContent("New Node Name", parent.GetUdi(), ContactUsForm.ModelTypeAlias);
Jannik Anker
  • 3,320
  • 1
  • 13
  • 21
0

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();
    }
BJ Patel
  • 6,148
  • 11
  • 47
  • 81
0

In Umbraco 8, you need the parent Udi to create a new node. You can do this by getting the parent node first then getting the Udi using the parent node like this:

var parentNode = ContentService.GetById(1100);
var parentUdi = new GuidUdi(parentNode.ContentType.ToString(), parentNode.Key);

You can then call the CreateContent method and pass in the parentUdi as a parameter:

var request = ContentService.CreateContent("New Node Name", parentUdi, ContactUsForm.ModelTypeAlias);
ContentService.SaveAndPublish(request);
Enkosi X
  • 403
  • 4
  • 11