New Taxonomies
I'm trying to think in Taxonomies, Categories and such and so went and created a new classification type, filled it with come categories and then decided I wanted to classify some generic content items with this new taxonomy but can't see where I would apply it. The GC has a place to set Categories and Tags but no where to choose the new Classification. Am I missing something?
Also a small bug (at least in the SDK sample site). From the main menu if you click Content-->Generic Content it takes you to the Generic Content's comment management page instead of directly to the list of Generic Contents.
Hello KMac,
Here is a sample code that shows how to create a new taxonomy type.
public
static
void
TryCreateNewTaxonomy(
string
taxonomyTitle,
string
taxonomyName, TaxonomyType type,
params
Type[] contentTypes)
if
(
string
.IsNullOrEmpty(taxonomyTitle))
throw
new
ArgumentNullException(
"taxonomyTitle"
);
if
(
string
.IsNullOrEmpty(taxonomyName))
throw
new
ArgumentNullException(
"taxonomyName"
);
var taxonomyManager = TaxonomyManager.GetManager();
// first check if the taxonomy of specified type and name already exists.
ITaxonomy taxonomy =
null
;
var iconCss =
string
.Empty;
switch
(type)
case
TaxonomyType.Flat:
taxonomy = taxonomyManager.GetTaxonomies<FlatTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
iconCss =
"sfFlatTaxonIcn"
;
break
;
case
TaxonomyType.Hierarchical:
taxonomy = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
iconCss =
"sfHierarchicalTaxonIcn"
;
break
;
case
TaxonomyType.Facet:
taxonomy = taxonomyManager.GetTaxonomies<FacetTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
break
;
case
TaxonomyType.Network:
taxonomy = taxonomyManager.GetTaxonomies<NetworkTaxonomy>().Where(t => t.Name == taxonomyName).SingleOrDefault();
break
;
// if the taxonomy of given name and specified type does not exist, we will create it here
if
(taxonomy ==
null
)
switch
(type)
case
TaxonomyType.Flat:
taxonomy = taxonomyManager.CreateTaxonomy<FlatTaxonomy>();
break
;
case
TaxonomyType.Hierarchical:
taxonomy = taxonomyManager.CreateTaxonomy<HierarchicalTaxonomy>();
break
;
case
TaxonomyType.Facet:
taxonomy = taxonomyManager.CreateTaxonomy<FacetTaxonomy>();
break
;
case
TaxonomyType.Network:
taxonomy = taxonomyManager.CreateTaxonomy<NetworkTaxonomy>();
break
;
taxonomy.Name = taxonomyName;
taxonomy.Title = taxonomyTitle;
taxonomyManager.SaveChanges();
// add dynamic fields to be used to store taxa from this new taxonomy
Manager.AddTaxonomyFieldToModel(taxonomy, taxonomyManager.Provider.Name, contentTypes);
// add the taxonomy control to the page editor toolbox
TaxonomyControlManager.AddTaxonomyControlToToolbox((Taxonomy)taxonomy, iconCss, taxonomy.Name);
// add user interface elements to the backend content forms
Manager.AddUserInterfaceElementToBackendForm(type, taxonomy, contentTypes);
private
static
void
AddTaxonomyFieldToModel(ITaxonomy taxonomy,
string
taxonomyProvider,
params
Type[] contentTypes)
MetaField field;
var metaMan = MetadataManager.GetManager();
foreach
(var contentType
in
contentTypes)
var type = metaMan.GetMetaType(contentType);
if
(type ==
null
)
type = metaMan.CreateMetaType(contentType);
var taxonomyName = taxonomy.Name;
var result = metaMan.GetMetafields().SingleOrDefault(f => f.FieldName == taxonomyName);
if
(result ==
null
)
field = metaMan.CreateMetafield(taxonomy.Name);
field.TaxonomyProvider = taxonomyProvider;
field.TaxonomyId = taxonomy.Id;
field.IsSingleTaxon =
false
;
type.Fields.Add(field);
metaMan.SaveChanges(
true
);
var controlToolbox = toolboxConfig.Toolboxes[
"PageControls"
];
var classificationSection = controlToolbox.Sections.Where<ToolboxSection>(s => s.Name ==
"Classifications"
).SingleOrDefault();
var existingControls = classificationSection.Tools.Where<ToolboxItem>(t => t.ControlType ==
typeof
(TaxonomyControl).FullName);
foreach
(var existingControl
in
existingControls)
if
(existingControl.Parameters[
"TaxonomyId"
] == taxonomy.Id.ToString())
return
;
var toolboxItem =
new
ToolboxItem(classificationSection.Tools)
ControlType =
typeof
(TaxonomyControl).FullName,
Name = taxonomy.Name,
Title = taxonomy.Title,
Description = taxonomy.Description,
Enabled =
true
,
CssClass = toolboxCssClass,
Parameters =
new
NameValueCollection()
"TaxonomyId"
, taxonomy.Id.ToString() ,
"FieldName"
, metaFieldName
;
classificationSection.Tools.Add(toolboxItem);
var taxonSection =
new
ContentViewSectionElement(detailView.Sections)
Name =
"TaxonSection"
,
Title = Res.Get<ModuleResources>().CustomType,
ResourceClassId =
typeof
(ModuleResources).Name,
CssClass =
"sfExpandableForm"
,
ExpandableDefinitionConfig =
Expanded =
false
;
// reusing CategoriesFieldWriteMode for demonsration - it is used for categories in content modules.
var HierarchicalTaxonElement = DefinitionTemplates.CategoriesFieldWriteMode(taxonSection.Fields);
HierarchicalTaxonElement.DisplayMode = displayMode;
taxonSection.Fields.Add(HierarchicalTaxonElement );
detailView.Sections.Add(taxonSection);
Hey Ivan,
Just to clarify when I said taxonomy, I meant a custom classification. Is this something that is going to work out of the box with the final release or do we have to do this every time we want to create a site with custom taxonomies. I just assumed that because you can make custom taxonomies through the UI it would automatically pick up these taxonomies, especially since editors can theoretically go in and create these taxonomies themselves.
And as far as the SDK, I'm using the version uploaded on the 22nd. (I'm not in front of my development machine so don't know the exact version).
Hi Ivan,
After reading through more of the developer manual, I think I was using the wrong term. I'm not looking to use a new taxomony class, just a new classification based on hierarchical lists. I created that new classification through the UI (Content --> All Classifications --> Create Classification) and populated it with items that make sense for my classification. Now all I want to do is tag some generic content or news items with this new classification set. Are they not just readily available after creating throught the UI? If not, will they be?
And I was wrong, I hadn't updated the SDK to 910 so it seems to be fixed.
Hello KMac,
We are working on an implementation that will allow you to add your custom classification to Edit and New views. Currently the only way to assign a content item to a given taxon is using SetTaxon Method
Kind regards,
Ivan Dimitrov
the Telerik team