1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Hierarchal taxonomy creation and sub category

Hierarchal taxonomy creation and sub category

6 posts, 1 answered
  1. Jesse
    Jesse avatar
    31 posts
    Registered:
    08 Nov 2010
    02 Feb 2011
    Link to this post
    Hey guys,

    I'm currently trying to create categories and sub categories from Visual Studio. My approach to this would have been registering a Custom widget through the site and well I'm using my .cs file with my code behind to create the categories. In reading through the development guide that was there and even used it in my code and it didn't work. Reference:- http://www.sitefinity.com/40/help/developer-manual/taxonomies-hierarchical-taxonomy-hierarchical-taxonomy-api.html 
    Its on a button click of a page the page that has the widget. My code is below here.

    private void SampleCode()
            {
                //Create a new hierarhical taxonomy using the TaxonomyManager class
                TaxonomyManager manager = TaxonomyManager.GetManager();
                var tax = manager.CreateTaxonomy<HierarchicalTaxonomy>();
                tax.Name = "News";
                tax.TaxonName = "News";
                tax.Title = "News";
                tax.Description = "Taxonomy which will classify the news on our web site";
                //Create a new taxon and add it to the taxonomy - Europe News
                var rootTaxonNews = manager.CreateTaxon<HierarchicalTaxon>();
                rootTaxonNews.Title = "From Europe";
                rootTaxonNews.Name = "European News";
                rootTaxonNews.Description = "This category holds the news from Europe";
                tax.Taxa.Add(rootTaxonNews);
                //Create two sub-taxa and add them to the European News taxon.
                //1
                var taxon1 = manager.CreateTaxon<HierarchicalTaxon>();
                taxon1.Title = "Politics";
                rootTaxonNews.Name = "Politics";
                taxon1.Description = "This category holds the Politics news";
                rootTaxonNews.Subtaxa.Add(taxon1);
                ////2
                //var taxon2 = manager.CreateTaxon<HierarchicalTaxon>();
                //taxon2.Title = "Entertainment";
                //taxon2.Name = "Entertainment";
                //taxon2.Description = "This category holds the Entertainment news";
                //rootTaxonNews.Subtaxa.Add(taxon2);
                //Create a new taxon and add it to the taxonomy - US News
                var rootTaxonUSNews = manager.CreateTaxon<HierarchicalTaxon>();
                rootTaxonUSNews.Title = "From USA";
                rootTaxonUSNews.Name = "USA News";
                rootTaxonUSNews.Description = "This category holds the news from USA";
                tax.Taxa.Add(rootTaxonUSNews);
                //Save all changes done up to now.
                manager.SaveChanges();
            }

    I used that and i got the error below. If possible can someone guide me as to why I'm getting that error or if I'm missing something when trying to save.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 189:            tax.Taxa.Add(rootTaxonUSNews);

    Line 190:            //Save all changes done up to now.

    Line 191:            manager.SaveChanges();

    Line 192:        }       

    Line 193:


    Source File: C:\Program Files (x86)\Telerik\Sitefinity 4.0\Projects\CopperSparrow\Custom\Widget\ImportDataSitefinity.ascx.cs    Line: 191

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]

       Telerik.Sitefinity.Security.AuthorizationPermissionProvider.AuthorizeInput(IMethodInvocation input) +3234

       Telerik.Sitefinity.Security.SitefinityAuthorizationCallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) +274

       Telerik.Microsoft.Practices.Unity.InterceptionExtension.HandlerPipeline.Invoke(IMethodInvocation input, InvokeHandlerDelegate target) +221

       Telerik.Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior.Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) +199

       Telerik.Microsoft.Practices.Unity.InterceptionExtension.InterceptionBehaviorPipeline.Invoke(IMethodInvocation input, InvokeInterceptionBehaviorDelegate target) +221

       DynamicModule.ns.Wrapped_OpenAccessTaxonomyProvider_6a25c2a10f38416f837755f51419943b.CommitTransaction() +181

       Telerik.Sitefinity.Data.ManagerBase`1.SaveChanges() +137

       SitefinityWebApp.Custom.Widget.ImportDataSitefinity.SampleCode() in C:\Program Files (x86)\Telerik\Sitefinity 4.0\Projects\CopperSparrow\Custom\Widget\ImportDataSitefinity.ascx.cs:191

       SitefinityWebApp.Custom.Widget.ImportDataSitefinity.Execute_Click(Object sender, EventArgs e) in C:\Program Files (x86)\Telerik\Sitefinity 4.0\Projects\CopperSparrow\Custom\Widget\ImportDataSitefinity.ascx.cs:100

       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118

       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112

       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10

       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13

       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36

       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563



  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    08 Feb 2011
    Link to this post
    Hello Jesse,

    You need to set Taxonomy property of the taxons:

    private void SampleCode()
            {
                //Create a new hierarhical taxonomy using the TaxonomyManager class
                TaxonomyManager manager = TaxonomyManager.GetManager();
                var tax = manager.CreateTaxonomy<HierarchicalTaxonomy>();
                tax.Name = "News";
                tax.TaxonName = "News";
                tax.Title = "News";
                tax.Description = "Taxonomy which will classify the news on our web site";
                //Create a new taxon and add it to the taxonomy - Europe News
                var rootTaxonNews = manager.CreateTaxon<HierarchicalTaxon>();
                rootTaxonNews.Title = "From Europe";
                rootTaxonNews.Name = "European News";
                rootTaxonNews.Description = "This category holds the news from Europe";
                rootTaxonNews.Taxonomy = tax;
                tax.Taxa.Add(rootTaxonNews);
                //Create two sub-taxa and add them to the European News taxon.
                //1
                var taxon1 = manager.CreateTaxon<HierarchicalTaxon>();
                taxon1.Title = "Politics";
                rootTaxonNews.Name = "Politics";
                taxon1.Description = "This category holds the Politics news";
                taxon1.Taxonomy = tax;
                rootTaxonNews.Subtaxa.Add(taxon1);
                ////2
                //var taxon2 = manager.CreateTaxon<HierarchicalTaxon>();
                //taxon2.Title = "Entertainment";
                //taxon2.Name = "Entertainment";
                //taxon2.Description = "This category holds the Entertainment news";
                //rootTaxonNews.Subtaxa.Add(taxon2);
                //Create a new taxon and add it to the taxonomy - US News
                var rootTaxonUSNews = manager.CreateTaxon<HierarchicalTaxon>();
                rootTaxonUSNews.Title = "From USA";
                rootTaxonUSNews.Name = "USA News";
                rootTaxonUSNews.Description = "This category holds the news from USA";
                rootTaxonUSNews.Taxonomy = tax;
                tax.Taxa.Add(rootTaxonUSNews);
                //Save all changes done up to now.
                manager.SaveChanges();
            }

    Let us know if this helps.

    Greetings,
    Pepi
    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
  3. Jess
    Jess avatar
    2 posts
    Registered:
    03 Feb 2011
    08 Feb 2011
    Link to this post
    Hey Pepi,

    Thanks for the response. I am able to create categories and sub-categories. It is working thanks again! I think you should include the lines that were missing in the development guide because it's missing there. Just a suggestion.
  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    10 Feb 2011
    Link to this post
    Hello Jess,

    The Parent taxon of the sub-taxon is not set and most probably that causes the problem:

    var taxon1 = manager.CreateTaxon<HierarchicalTaxon>();
    taxon1.Title = "Politics";
    rootTaxonNews.Name = "Politics";
    taxon1.Description = "This category holds the Politics news";
    taxon1.Taxonomy = tax;
    taxon1.Parent = rootTaxonNews;
    rootTaxonNews.Subtaxa.Add(taxon1);

    Do let us know if this solves the issue.

    Greetings,
    Pepi
    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
    Answered
  5. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    29 Mar 2012
    Link to this post
    Yes, please add this to the documentation since it is still missing. Also you will have to remember to set the UrlName for it be created properly: 

    taxon.UrlName = Regex.Replace(name.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);

    PS - Also please add UrlNameCharsToReplace and UrlNameReplaceString as global constants to the framework :)
  6. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    29 Mar 2012
    Link to this post
    <<double post>>
6 posts, 1 answered