2013-10-23 60 views
8

我正在使用umbraco 6.1.6。我想知道如何以編程方式將節點添加到內容樹中?Umbraco:使用C#在內容中創建子節點

這是我想要的結構:

內容

  • 首頁
    • 開始
    • 壓延
    • 頭版滑塊
    • 照片
    • 新聞

這裏的子節點具有相同的文檔類型。我如何使用C#編程式創建這些子節點?

回答

11

試試這個,

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

OR

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

希望這有助於

+2

只爲信息:我怎樣才能得到一個內容我剛剛creaerd的ID? SaveAndPublish方法沒有重載任何Id – Ras

+1

@Ras ContentService.CreateContent返回一個IContent實例,它有一個名爲Id的屬性,它指向新創建的內容的Id。 –

相關問題