2013-10-23 13 views
8

Tôi đang làm việc với umbraco 6.1.6. Tôi muốn biết cách thêm một nút vào cây nội dung theo chương trình?Umbraco: tạo mã con trong nội dung bằng cách sử dụng C#

Đây là cấu trúc tôi muốn:

nội dung

  • Home
    • bắt đầu
    • lịch
    • thanh trượt frontpage
    • fotos
    • tin tức

Ở đây mã con có cùng loại tài liệu. Làm thế nào tôi có thể tạo ra các childnodes programatically bằng cách sử dụng C#?

Trả lời

11

Hãy thử điều này,

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

HOẶC

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

Hope this helps

+2

Chỉ cần cung cấp thông tin: làm thế nào tôi có thể có được ID của một nội dung tôi vừa creaerd? Không có quá tải cho phương thức SaveAndPublish trả về bất kỳ Id – Ras

+1

@Ras ContentService.CreateContent trả về một phiên bản IContent và nó có một thuộc tính được gọi là Id trỏ đến Id nội dung mới được tạo của bạn. –

2

Bạn cần sử dụng API của Umbraco cho điều đó. Điều này được đề cập trong tài liệu Umbraco tại đây: http://our.umbraco.org/documentation/Reference/

Tóm lại, bạn có thể sử dụng phương pháp CreateContent() của API ApplicationContext.Current.Services.ContentService.

Các vấn đề liên quan