Bulk add nodes / content in Umbraco

Wednesday, May 19, 2010 by Kristian

This tutorial show how to add a desktop app to Umbraco for adding multiple nodes to a given parent.

First the front end usercontrol:

Then the codebehind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;

namespace HeltBlank.Packages.dashboards
{
public partial class BulkAddContent : System.Web.UI.UserControl
{
public umbraco.controls.ContentPicker cp = new umbraco.controls.ContentPicker();

protected override void OnInit(EventArgs e)
{
base.OnInit(e);

//Add the content picker for the parent node
ph_contentPicker.Controls.Add(cp);

//List of DocumentTypes
drpType.DataSource = DocumentType.GetAllAsList();
drpType.DataTextField = "Alias";
drpType.DataBind();
}

protected void create(object sender, EventArgs e)
{
//You could fix some better error handling, but I don't bother :-)
try
{
//Find the document type
DocumentType dt = DocumentType.GetByAlias(drpType.Text);

int parent = !String.IsNullOrEmpty(cp.Text) ? int.Parse(cp.Text) : -1;

//Pares the nodes (seperated with ;
string[] nodes = txt.Text.Trim().Split(';');
int counter = 0;

//Run trough each node
foreach (string s in nodes)
{
if (!string.IsNullOrEmpty(s.Trim()))
{
//Create it as a document
Document d = Document.MakeNew(s.Trim(), dt, User.GetCurrent(), parent);

d.Save();
d.Publish(User.GetCurrent());

umbraco.library.UpdateDocumentCache(d.Id);

counter++;
}
}

//Clear and status
txt.Text = string.Empty;
status.Text = "Added " + counter + " nodes";
}
catch (Exception err) { status.Text = "Error: " + err.Message; }
}
}
}

At the end you should update your dashboard.config with the new usercontrol:

And the control is up and running :-)

BulkAdd

Idea from blog4umbraco desktop app

Source files