Add Tags and Categories to Sitefinity then add it to a post

Posted by Community Admin on 05-Aug-2018 16:24

Add Tags and Categories to Sitefinity then add it to a post is not working

All Replies

Posted by Community Admin on 01-Feb-2012 00:00

I need to add Tags and Categories to Sitefinity dynamiclly i'm using the following code

For Categories

var catTaxon = manager.GetTaxonomies<Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxonomy>().Where(query =>
                  String.Compare(query.Name, "Categories", true) == 0).FirstOrDefault();
catTaxon.Taxa.Add(manager.GetTaxon<Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon>(taxonomy.Id));


and for Tags

var tagTaxon = manager.GetTaxonomies<Telerik.Sitefinity.Taxonomies.Model.FlatTaxonomy>().Where(query =>
                    String.Compare(query.Name, "Tag", true) == 0).FirstOrDefault();
tagTaxon.Taxa.Add(manager.GetTaxon<Telerik.Sitefinity.Taxonomies.Model.FlatTaxon>(taxonomy.Id));
 
manager.SaveChanges();


the taxonomy is the new item i need to add.

and it gives me the following exception

You are trying to access item that no longer exists. The most probable reason is that it has been deleted by another user.

also the tags and categories i want to save , it is got saved under general classification.

Posted by Community Admin on 06-Feb-2012 00:00

Hi,

 This is the API that will craete new taxa in categories taxonomy, the approach is relevant for Tags.

var manager = TaxonomyManager.GetManager();
//here take the CategoriesTaxonomyId, also you may take TagsTaxonomyId
            var CategoriesTaxonomy = manager.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId);
            var newDepartment = manager.CreateTaxon<HierarchicalTaxon>();
            newDepartment.Title = "From Europe";
            newDepartment.Name = "European ";
            newDepartment.Description = "This is Europe";
            CategoriesTaxonomy.Taxa.Add(newDepartment);
            manager.SaveChanges();

The query is not construced to suit the purpose for Adding taxons to existing taxonomy and I suppose this is causing the exception.

To Query the taxons use this API:
TaxonomyManager manager = TaxonomyManager.GetManager();
// p.Title gets the name of one new taxonomy
           var taxonomy = manager.GetTaxonomies<HierarchicalTaxonomy>().Where(p => p.Title == "Sports").OrderBy(r => "Ordinal").Single().Taxa.ToList();
this will return a list of all taxonomies ordered as they are ordered in the backend of Sitefinity (Ordinal is the property).



Greetings,
Stanislav Velikov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Posted by Community Admin on 06-Feb-2012 00:00

Hi,
thanks for your previous replay,
i was able to save the tags and categories successfully.
but the problem now is to link a post to a category or tag

and below is the code that i use
var manager = Telerik.Sitefinity.Taxonomies.TaxonomyManager.GetManager();
                var taxon = manager.GetTaxa<Telerik.Sitefinity.Taxonomies.Model.HierarchicalTaxon>().Where(query =>
                    query.Name == category.Name).FirstOrDefault();
 
                var blogPost = this._sitefinityAPI.BlogPosts().Where(query => query.Id == postId).First();
                blogPost.CheckOut()
                    .Do(item =>
                    
                        if (!item.Organizer.TaxonExists("Category", taxon.Id))
                            item.Organizer.TaxonExists("Category", taxon.Id);
                    )
                    .CheckIn()
                    .Publish()
                    .SaveChanges();

it keep giving me null reference exception.

Posted by Community Admin on 09-Feb-2012 00:00

Hello,

 Review how taxa is associated with Item here. Organized.Addtaxa can be called when creating a blog post or in a list

BlogsManager blogsManager = BlogsManager.GetManager();
             
            BlogPost bb = blogsManager.CreateBlogPost();
            bb.Organizer.AddTaxa(...);

So to add a tag to Blog post that is already created:
var taxManager = TaxonomyManager.GetManager();
           var taxon = taxManager.GetTaxa<FlatTaxon>().Where(t => t.Name == "Book").Single();
           BlogsManager blogsManager = BlogsManager.GetManager();
           var myPost = blogsManager.GetBlogPosts().Where(d => d.Title == "Post1");
           foreach(var doc in myPost)
           
               doc.Organizer.AddTaxa("Tags", taxon.Id);
           
           blogsManager.SaveChanges();


All the best,
Stanislav Velikov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

This thread is closed