【问题标题】:Create new content nodes programmatically in Umbraco 8在 Umbraco 8 中以编程方式创建新的内容节点
【发布时间】:2019-04-23 14:37:14
【问题描述】:

在 Umbraco 7 中,我使用以下代码从 C#(控制器)以编程方式生成代码

使用 ContentService.CreateContent 以下是相同的代码

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

现在在 Umbraco 8

它要求

Udi 父 ID

出现错误“无法将 'int' 转换为 'Umbraco.Core.Uid'”。

搜索了很多,但找不到任何关于 Umbraco 8 的内容。

所以现在的问题是我们如何在 Umbraco 8 中从控制器创建节点?

【问题讨论】:

    标签: umbraco umbraco-contour umbraco8


    【解决方案1】:

    先获取父节点(这可以通过 int ID 完成)然后从中获取 UDI 怎么样?类似的东西

    var parent = ContentService.GetById(1100);
    var request = ContentService.CreateContent("New Node Name", parent.GetUdi(), ContactUsForm.ModelTypeAlias);
    

    【讨论】:

      【解决方案2】:

      解决方案如下链接所示

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

      【讨论】:

        【解决方案3】:

        在 Umbraco 8 中,您需要父 Udi 来创建新节点。您可以先获取父节点,然后使用父节点获取 Udi,如下所示:

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

        然后您可以调用 CreateContent 方法并将 parentUdi 作为参数传入:

        var request = ContentService.CreateContent("New Node Name", parentUdi, ContactUsForm.ModelTypeAlias);
        ContentService.SaveAndPublish(request);
        

        【讨论】:

          猜你喜欢
          • 2014-08-02
          • 1970-01-01
          • 1970-01-01
          • 2015-12-27
          • 2014-02-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多