Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nick-hoang/15fd6bc02b08471546cee95d50cf63c5 to your computer and use it in GitHub Desktop.
Save nick-hoang/15fd6bc02b08471546cee95d50cf63c5 to your computer and use it in GitHub Desktop.
Umbraco 7: create new content type
var contentTypeService = ApplicationContext.Current.Services.ContentTypeService;
const string contentTypeAlias = "spacerSection";
var section = contentTypeService.GetContentType(contentTypeAlias);
if(section == null)
{
var abstractSection = contentTypeService.GetContentType("abstractSectionTheme");
section = new ContentType(-1)
{
Key = Guid.NewGuid(),
Alias = contentTypeAlias,
Name = "Spacer Section",
Description = "Add spaces between sections",
ContentTypeComposition = new List<IContentTypeComposition> { abstractSection },
Icon = "icon-frame-alt color-orange",
PropertyGroups = new PropertyGroupCollection(new List<PropertyGroup> { new PropertyGroup() { Name = "Content" } }),
UpdateDate = DateTime.Now,
CreateDate = DateTime.Now
};
var dataTypes = FindAndCreateDataTypeByName("Numeric - greater than 0", "Checkbox");
AddPropertyType(section, dataTypes["Numeric - greater than 0"], "height", "Height", "In pixel", "Content", 0);
contentTypeService.Save(section);
//add as allowed child node type of "Page Elements"
var pageElements = contentTypeService.GetContentType("pageElements");
if (pageElements != null)
{
var existingAllowedContentTypes = pageElements.AllowedContentTypes != null ? pageElements.AllowedContentTypes.ToList() : new List<ContentTypeSort>();
var newSortOrder = existingAllowedContentTypes.Any() ? existingAllowedContentTypes.Max(e => e.SortOrder) + 1 : 0;
existingAllowedContentTypes.Add(new ContentTypeSort(new Lazy<int>(() => section.Id), newSortOrder, section.Alias));
pageElements.AllowedContentTypes = existingAllowedContentTypes;
contentTypeService.Save(pageElements);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment