Add a new classification dynamically
Hello,
I develop a custom module based on Product catalog sample. To complete its data model, i create 2 new classifications in sitefinity back office and add 2 custom fields to my custom module. It is possible to create these classifications during module is installing?
Thanks
Jocelyn
Hi jocelyn,
It can be done. In your module you can override InstallTaxonomies method and create your custom classification.
You will find a sample code here.
Greetings,
Ivan Dimitrov
the Telerik team
Hello Ivan,
Thanks for your reply.
Here is the code i'm using:
protected
override
void
InstallTaxonomies(SiteInitializer initializer)
this
.InstallTaxonomy(initializer,
typeof
(ContactItem));
var taxonomyManager = TaxonomyManager.GetManager();
FlatTaxonomy countries = taxonomyManager.CreateTaxonomy<FlatTaxonomy>();
countries.Name =
"Countries"
;
countries.Title =
"Countries"
;
taxonomyManager.SaveChanges();
var metaMan = initializer.Context.MetadataManager;
var type = metaMan.CreateMetaType(
typeof
(ContactItem));
var field = metaMan.CreateMetafield(
"Country"
);
field.TaxonomyProvider = taxonomyManager.Provider.Name;
field.TaxonomyId = countries.Id;
field.IsSingleTaxon =
true
;
type.Fields.Add(field);
metaMan.SaveChanges();
Insert of '645785618-bbc609a5-1923-402e-a7e5-6cd07639e415' failed: Telerik.OpenAccess.RT.sql.SQLException:
Impossible to insert a line of key in double into the object ' dbo.sf_meta_types ' with a unique index ' idx_sf_meta_types '. The instruction was stopped.
à Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.execute()
à OpenAccessRuntime.Relational.conn.PooledPreparedStatement.execute()
à OpenAccessRuntime.Relational.RelationalStorageManager.generateInserts(NewObjectOID oid, Int32 index, ClassMetaData cmd, PersistGraph graph, Int32[] fieldNos, CharBuf s, Object[] oidData, IntArray toUpdateIndexes)
INSERT INTO [sf_meta_types] ([id], [app_name], [assembly_name], [base_class_name], [class_name], [database_inheritance], [is_dynamic], [name_space], [section_captions_resource_type], [voa_version]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(set event logging to all to see parameter values)
Hi jocelyn,
You can try using the code below
public
static
readonly
Guid CountriesId =
new
Guid(
"DE0F3A19-A200-48a7-88EC-34495C0F5321"
);
protected
override
void
InstallTaxonomies(SiteInitializer initializer)
var taxMan = initializer.TaxonomyManager;
var flatTaxonomy =
this
.GetOrCreateTaxonomy<FlatTaxonomy>
(initializer,
"Countries"
, MyModuleManager.CountriesId, Res.Get<CountriesResources>().Countries);
MetaField field;
var metaMan = initializer.Context.MetadataManager;
var type = metaMan.GetMetaType(itemType);
if
(type ==
null
)
type = metaMan.CreateMetaType(itemType);
var result = type.Fields.SingleOrDefault(f => f.FieldName ==
"Countries"
);
var reflProp = itemType.GetProperty(Countries);
if
(result ==
null
&& reflProp ==
null
)
field = metaMan.CreateMetafield(
"Countries"
);
field.TaxonomyProvider = taxMan.Provider.Name;
field.TaxonomyId = flatTaxonomy.Id;
field.IsSingleTaxon =
false
;
var flatTaxonFieldCountries = ControlUtilities.GetSitefinityTextResource(
"Telerik.Sitefinity.Samples.Templates.Fields.FrontendFlatTaxonFieldCountry.htm"
);
var scaffoldInfoMetaAttr =
new
MetaFieldAttribute
Name =
"ControlTag"
,
Value = flatTaxonFieldTag
;
field.MetaAttributes.Add(scaffoldInfoMetaAttr);
field.MetaAttributes.Add(
new
MetaFieldAttribute Name =
"IsCommonProperty"
, Value =
"true"
);
type.Fields.Add(field);
<
sitefinity:FlatTaxonField
ID
=
"FlatFieldControl"
DisplayMode
=
"Read"
runat
=
"server"
WebServiceUrl
=
"~/Sitefinity/Services/Taxonomies/FlatTaxon.svc"
AllowMultipleSelection
=
"true"
TaxonomyId
=
"DE0F3A19-A200-48a7-88EC-34495C0F5321"
TaxonomyMetafieldName
=
"Countries"
Expanded
=
"false"
ExpandText
=
"ClickToAddCountries"
BindOnServer
=
"true"
/>
Hello,
I used the following code to add my taxonomy while installing my custom module.
protected
void
InstallCustomTaxonomies(SiteInitializer initializer)
var metaMan = initializer.Context.MetadataManager;
var taxMan = initializer.TaxonomyManager;
var hirerarchicalTaxonomy =
this
.GetOrCreateTaxonomy<HierarchicalTaxonomy>
(initializer,
"Markets"
, MarketsTaxonomyId,
"Market"
);
var type = metaMan.CreateMetaType(
typeof
(ActuatorsItem));
var field = metaMan.CreateMetafield(
"Markets"
);
field.TaxonomyProvider = taxMan.Provider.Name;
field.TaxonomyId = MarketsTaxonomyId;
field.IsSingleTaxon =
false
;
type.Fields.Add(field);
initializer.Context.MetadataManager.SaveChanges(
false
);
private
TTaxonomy GetOrCreateTaxonomy<TTaxonomy>(SiteInitializer initializer,
string
taxonomyName, Guid taxonomyId,
string
taxonName) where TTaxonomy :
class
, ITaxonomy
TTaxonomy taxonomy = initializer.Context.GetSharedObject<TTaxonomy>(taxonomyName);
if
(taxonomy ==
null
)
var taxMan = initializer.TaxonomyManager;
taxonomy = taxMan.GetTaxonomies<TTaxonomy>().FirstOrDefault(t => t.Name == taxonomyName);
if
(taxonomy ==
null
)
taxonomy = taxMan.CreateTaxonomy<TTaxonomy>(taxonomyId);
taxonomy.Name = taxonomyName;
taxonomy.Title = taxonomyName;
taxonomy.TaxonName = taxonName;
((SecModel.ISecuredObject)taxonomy).CanInheritPermissions =
true
;
((SecModel.ISecuredObject)taxonomy).InheritsPermissions =
true
;
((SecModel.ISecuredObject)taxonomy).SupportedPermissionSets =
new
string
[] SecurityConstants.Sets.Taxonomies.SetName ;
initializer.Context.SetSharedObject(taxonomyName, taxonomy);
return
taxonomy;
taxonomy.Title = taxonomyName;
: Up!
Can someone has any idea?
I have more information. I remove all the front languages so my website is not multilingual. Doing this, it works. Why, in case of multilingual website it does not work?
Jocelyn