Add a new classification dynamically

Posted by Community Admin on 03-Aug-2018 18:00

Add a new classification dynamically

All Replies

Posted by Community Admin on 29-Jan-2011 00:00

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

Posted by Community Admin on 31-Jan-2011 00:00

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

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 31-Jan-2011 00:00

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();
       

With this code, i have a first error on line 'countries.Title = "Countries"': Unsupported language "fr-FR". If you want to support this language, please configure your application accordingly.

If I comment this line, i have the following error (DuplicateKeyException):
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)

Thanks for your help!

Jocelyn

Posted by Community Admin on 03-Feb-2011 00:00

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" />

It looks like you have already added the key for your taxonomy.

All the best,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 11-Feb-2011 00:00

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;
        

I got the following error on executing the line taxonomy.Title = taxonomyName;:
Unsupported language "en-US". If you want to support this language, please configure your application accordingly.

Do you have any idea of this issue?
Could you please reply as soon as possible.

Regards,
Jocelyn

Posted by Community Admin on 12-Feb-2011 00:00

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

This thread is closed