Programmatically Add Nodes to Umbraco

//class for handling the document Object 

public class DocumentObject
{
    public
string TemplateName{get;set;}
    public
string NodeName{get;set;}
    // this should be the node alias and the values

    public
Dictionary<string, string> NodeItems{get;set;}
    public
int ParentNodeId { get; set; }
    public
string DocumentName { get; set; }
}
public
int AddNodesToUmbraco(DocumentObject documentObject)
{
    //what kind of document is the published nodes

    DocumentType
documentType = DocumentType.GetByAlias(documentObject.DocumentName);
    umbraco.cms.businesslogic.member.Member currentMember =    umbraco.cms.businesslogic.member.Member.GetCurrentMember();
    Document
doc = Document.MakeNew(documentObject.NodeName, documentType, currentMember.User, documentObject.ParentNodeId);
    foreach
(var pair in documentObject.NodeItems)
    {
        var
propertyType = doc.getProperty(pair.Key).PropertyType.DataTypeDefinition.Text;
        if
(pair.Value != "")
        {

           //checking whether it is a check box or not
           if
(propertyType.ToUpper().Contains("TRUE/FALSE"))
           {
              doc.getProperty(pair.Key).Value = pair.Value.ToUpper() == "YES" ? true : false;
           }

           else
           {

             doc.getProperty(pair.Key).Value = pair.Value;
           }
     }
}

if (documentObject.TemplateName != null)
{
    var alternateTemplate = 0;
    var templates = umbraco.cms.businesslogic.template.Template.GetAllAsList();
    foreach (var t in templates.Where(t => t.Alias == documentObject.TemplateName))
    alternateTemplate = t.Id;
    doc.Template = alternateTemplate;
}
doc.Save();
bool success = doc.PublishWithResult(currentMember.User);
umbraco.library.UpdateDocumentCache(doc.Id);
return doc.Id;//return id of the newly created item
}

 

Ebook Download
View all
Learn
View all