Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeriks/c4a415d519ff6ac5bf68 to your computer and use it in GitHub Desktop.
Save joeriks/c4a415d519ff6ac5bf68 to your computer and use it in GitHub Desktop.
Poor mans "Code First" implementation for Umbraco - or rather a sample of a Structure and Content creation script
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = null;
}
@functions{
/*
* Poor mans "Code First" implementation for Umbraco
*
* or rather - a basic script to create structure and nodes
* with support for basic document type creation (including tabs) and content
*
* Somewhat supporting updating existing structure / content
*
* Save as a template, for example dev_create. Then run it as www.mysite.com,/dev_create
*
* */
protected override void InitializePage()
{
//
// a list of reusable properties - the "type" is the name of the datatype found in umbraco
//
var properties = new[]{
new { alias = "introduction", name = "Introduction", type = "Richtext editor", tab="Content"},
new { alias = "bodyText", name = "Body Text", type = "Richtext editor", tab="Content"},
new { alias = "contentImage", name = "Content Image", type = "Media Picker", tab="Content"}};
//
// a list of the content types to create
//
var contentTypes = new[]{
new { alias = "root", name = "Web Site Root", description="Web site root", parent = "", allowedChildContentTypes = "additionalTextPage", properties = "", tabs = "Content"},
new { alias = "additionalTextPage", name = "Additional Text Page", description="Additional text page", parent = "", allowedChildContentTypes = "additionalTextPage", properties = "introduction, bodyText, contentImage", tabs = "Content"}};
//
// a list of the content to create - the "internalAlias" is used only by this routine to keep reference to nodes while creating the nodes
//
var contentItems = new[]{
new { name = "New Root", parentAlias = "", contentTypeAlias = "root", internalAlias="newRoot", properties = new Dictionary<string,string>()},
new { name = "Foo", parentAlias = "newRoot", contentTypeAlias = "additionalTextPage", internalAlias="", properties = new Dictionary<string, string> { { "introduction", "Intro xaz" }, { "bodyText", "Lorem ipsum 1" } }},
new { name = "Bar", parentAlias = "newRoot", contentTypeAlias = "additionalTextPage", internalAlias="", properties = new Dictionary<string, string> { { "introduction", "Intro yux" }, { "bodyText", "Lorem ipsum 1" } }}
};
var contentTypeService = ApplicationContext.Services.ContentTypeService;
var contentService = ApplicationContext.Services.ContentService;
var internalAliasList = new Dictionary<string, int>();
contentTypes.ToList().ForEach(t =>
{
var existingContentType = contentTypeService.GetContentType(t.alias);
if (existingContentType == null)
{
var parent = contentTypeService.GetContentType(t.parent);
var parentId = (parent != null) ? parent.Id : -1;
var contentType = new ContentType(parentId) { Alias = t.alias, Name = t.name, Description = t.description };
contentTypeService.Save(contentType);
}
else
{
var isChanged = false;
if (t.name != null) {
isChanged = true;
existingContentType.Name = t.name;
}
if (t.description != null) {
isChanged = true;
existingContentType.Description = t.description;
}
if (t.allowedChildContentTypes != null)
{
var allowedContentTypes = t.allowedChildContentTypes.Split(',');
var allowedContentTypeSorts = allowedContentTypes.Select((alias, index) =>
{
var contentType = contentTypeService.GetContentType(alias);
if (contentType != null)
{
return new ContentTypeSort(contentType.Id, index);
}
return new ContentTypeSort();
});
if (allowedContentTypeSorts!=null) {
existingContentType.AllowedContentTypes = allowedContentTypeSorts;
isChanged = true;
}
}
foreach (var tab in t.tabs.Split(',')){
existingContentType.AddPropertyGroup(tab);
isChanged = true;
}
foreach(var property in t.properties.Split(',')){
var prop = properties.SingleOrDefault(p => p.alias == property.Trim());
if (prop!=null){
var dataType = ApplicationContext.Services.DataTypeService.GetDataTypeDefinitionByName(prop.type);
var propertyType = new PropertyType(dataType){Alias = prop.alias, Name = prop.name};
existingContentType.AddPropertyType(propertyType, prop.tab);
isChanged = true;
}
}
if (isChanged)
contentTypeService.Save(existingContentType);
}
});
contentItems.ToList().ForEach(c =>
{
var parentId = -1;
if (c.parentAlias != "" && internalAliasList.ContainsKey(c.parentAlias))
{
parentId = internalAliasList[c.parentAlias];
}
var content = (parentId == -1) ? (contentService.GetRootContent().SingleOrDefault(t => t.Name == c.name)) : contentService.GetById(parentId).Children().SingleOrDefault(t => t.Name == c.name);
if (content == null)
{
content = contentService.CreateContent(c.name, parentId, c.contentTypeAlias);
}
foreach (var prop in c.properties)
{
var existingProperty = content.Properties[prop.Key];
if (existingProperty != null && existingProperty.Value != prop.Value)
{
existingProperty.Value = prop.Value;
}
}
contentService.Save(content);
if (c.internalAlias != "") internalAliasList[c.internalAlias] = content.Id;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment