gpt4 book ai didi

c# - Umbraco:使用 C# 在内容中创建子节点

转载 作者:可可西里 更新时间:2023-11-01 09:03:05 26 4
gpt4 key购买 nike

我正在使用 umbraco 6.1.6。我想知道如何以编程方式将节点添加到内容树中?

这是我想要的结构:

内容

  • 首页
    • 开始
    • 日历
    • 首页 slider
    • 照片
    • 新闻

这里的子节点具有相同的文档类型。如何使用 C# 以编程方式创建这些子节点?

最佳答案

试试这个,

using umbraco.cms.businesslogic.web;

DocumentType dt = DocumentType.GetByAlias("alias");

// The umbraco user that should create the document,
// 0 is the umbraco system user, and always exists
umbraco.BusinessLogic.User u = new umbraco.BusinessLogic.User(0);

//Replace 1055 with id of parent node
Document doc = Document.MakeNew("new child node name", dt, u, 1055);

//after creating the document, prepare it for publishing
doc.Publish(u);

//Tell umbraco to publish the document
umbraco.library.UpdateDocumentCache(doc.Id);

using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.Services;

// Get the Umbraco Content Service
var contentService = Services.ContentService;
var product = contentService.CreateContent(
"my new presentation", // the name of the product
1055, // the parent id should be the id of the group node
"Presentation", // the alias of the product Document Type
0);

// We need to update properties (product id, original name and the price)
product.SetValue("title", "My new presentation");

// finally we need to save and publish it (which also saves the product!)
// - that's done via the Content Service
contentService.SaveAndPublish(product);

希望对你有帮助

关于c# - Umbraco:使用 C# 在内容中创建子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19534687/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com