2013-10-23 8 views
8

Sto lavorando con umbraco 6.1.6. Voglio sapere come aggiungere un nodo nell'albero dei contenuti a livello di programmazione?Umbraco: creare childnodes nel contenuto utilizzando C#

Questa è la struttura che voglio:

contenuti

  • casa
    • inizio
    • calendario
    • cursori di FrontPage
    • foto
    • notizie

Qui i childnode hanno gli stessi tipi di documento. Come posso creare questi childcode programmaticamente usando C#?

risposta

11

Prova questo,

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

O

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

Spero che questo aiuti

+2

Solo per informazioni: come posso ottenere l'ID di un tenore che ho appena creaerd? Non ci sono sovraccarichi per il metodo SaveAndPublish che restituisce qualsiasi ID – Ras

+1

@Ras ContentService.CreateContent restituisce un'istanza di IContent e ha una proprietà denominata Id che punta all'ID del nuovo contenuto creato. –

Problemi correlati