API to add taxonomies in Custom Fields

Posted by Community Admin on 04-Aug-2018 19:04

API to add taxonomies in Custom Fields

All Replies

Posted by Community Admin on 16-Nov-2011 00:00
Hey, I'm trying to figure out how can I add custom fields for taxo to news, events, blogs, or any other type of content using API's..
Posted by Community Admin on 21-Nov-2011 00:00
Hello Nathalia,


Thank you for using our services. Can you please clarify whether you want to populate the current metafields for Tags and Categories of the content modules through code, or you want to implement different functionality. For operating with any of the Sitefinity modules' s custom fields you can use our extension methods GetValue() and SetValue() (please add a reference to Telerik.Sitefinity.Model in your class). I believe you might also find this article from our documentation useful. Please let us know if the functionality you want to implement differs from what I've described above, or you have any additional questions.


Regards,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested 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 21-Nov-2011 00:00
I pretend to add new properties of Tags and Categories in the customer fields in the content modules using API's. (Attached image shows where the custom fields are added).
I can add Tags and Categories using:
         module.Organizer.AddTaxa("categorynews", taxonId);
if the taxa that are adding the module does not exist in the custom fields this error occurs:
         "The specified property 'categorynews' does not exist on type 'Telerik.Sitefinity.News.Model.NewsItem'." 

The "categorynews" is a category that I add.


Thanks!
Posted by Community Admin on 23-Nov-2011 00:00
Hello Nathalia,

The easiest way to manipulate data stored inside custom fields would be by using the GetValue() and SetValue() extension methods that we mentioned in our previous reply. For instance, please find below a code sample which adds a certain category to all Ecommerce products:
TaxonomyManager manager = TaxonomyManager.GetManager();
            var pManager = CatalogManager.GetManager();
            var myCategoryId = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == "TT").Single().Id;
            var productList = pManager.GetProducts().ToList();
            foreach (var product in productList)
            
                var catlist = product.GetValue<TrackedList<Guid>>("Department");
                catlist.Add(myCategoryId);
                product.SetValue("Department", catlist);
            
            pManager.SaveChanges();


Greetings,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested 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 24-Nov-2011 00:00
Hi Boyan, thanks for your reply.  

The sample code provided in previous email assume that "Department" category already exist associated to Products Module so far so good, however If I use same code but for News then I'll received an unhandled exception "The specified property 'Departments' does not exist on type 'Telerik.Sitefinity.News.Model.NewsItem'."... question is:

How can I associated "Departments" as a valid News Field using API's?

For now my code can only avoid this issue by skipping this taxonomies values when they belong to a not associated field:

NewsItem item = manager.CreateNewsItem();
...
if (item.DoesFieldExist('Department")) // if the field exists.
                            
     if (!item.Organizer.TaxonExists("Department", valueID))
          item.Organizer.AddTaxa('Department", valueID); //Add taxonomy                                  


Regards,
Nathalia
Posted by Community Admin on 25-Nov-2011 00:00
Hi Nathalia,

Let me elaborate a little bit on the default taxonomies functionality in Sitefinity.

By default you have two types of classifications - Hierarchical Taxonomy and Flat Taxonomy. Flat taxonomies(as the name suggests) have a flat structure (e.g. they do not support parent-child hierarchy). An example of the Flat taxonomies in Sitefinity is Tags - you have the base Flat Taxonomy Tags, and all tags you've assigned to your content are stored under Tags.
Categories, on the other side are an example of Hierarchical taxonomy - you can have a Parent Category, and then Child category, which allows you to structure your content hierarchically. Departments is another example of Hierarchical taxonomies, which is specific for content items of type Product (since it makes more sense to group your products in different departments).
 You can create your own classifications as well, besides the default ones, and assign your content items to then, what's important to know is that you need to specify the correct type when working with classifications through code.

Let's take for example News. If you want to get all the categories that are assigned to a News item, you'll need to get the value of the metafiled Category, and this value will be a list of Category IDs. In the same line of reasoning, you can get all the Departments, to which a product belongs by getting the value of the metafield "Departments". Let's take the code sample we provided in our previous reply and explain it in more details, I believe this might be useful:
//First get the manager which operates with Taxonomies
TaxonomyManager manager = TaxonomyManager.GetManager();
//Get the manager corresponding to the Content type you'll be operating with
CatalogManagerpManager = CatalogManager.GetManager();
//Get the ID of a singe instance (HierarchicalTaxon) of type Hierarchicaltaxonomy
//(This can be Categories, Departments, or any custom HierarchicalTaxonomy)
            var myCategoryId = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == "TT").Single().Id;
//Get a list of content items of the desired Content type
            var productList = pManager.GetProducts().ToList();
//For each of the content items in the above constructed list
            foreach (var product in productList)
            
//Get all the IDs of the HierarchicalTaxons in the HierarchicalTaxonomy "Departments"
// which is stored in the "Department metafield of this content item"
                var catlist = product.GetValue<TrackedList<Guid>>("Department");
//modify the list as per your requirements
                catlist.Add(myCategoryId);
//assign the modified list to the "Department metafield of this content item
//this is equivalent to adding a new Department to a product through the UI
                product.SetValue("Department", catlist);
            
//Commit the changes
            pManager.SaveChanges();

in that line of reasoning the same sample can be easily implemented for News items like this:
TaxonomyManager manager = TaxonomyManager.GetManager();
            NewsManager nManager = NewsManager.GetManager();
            var myCategoryId = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name == "MyNewsCategory").Single().Id;
            var newsList = nManager.GetNewsItems().ToList();
            foreach (var newsItem in newsList)
            
                var catlist = newsItem.GetValue<TrackedList<Guid>>("Category");
                catlist.Add(myCategoryId);
                newsItem.SetValue("Category", catlist);
            
            nManager.SaveChanges();

I believe you might find the Taxonomies section from our Documentation very useful in terms of presenting the theoretical base behind classifications in Sitefinity. Please do not hesitate to let us know if you need some further information.

Regards,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested 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 25-Nov-2011 00:00

Hi Boyan,

Thanks for your information, perhaps I didn't explain well and I used taxonomies as a bad example.  It's not Taxonomies but "Custom Fields".  Now, I know how add "Custom Fields" throughout Sitefinity Back-End (http://www.sitefinity.com/documentation/user-guide/creating-and-uploading-content/adding-custom-fields-to-content-items/creating-a-new-custom-field.aspx) but how can I do the same process using API's?   Is there any way to do that?

 

I used taxonomies at the beginning since they could also added as “Custom Fields”.

 

Thanks again, sorry for confusion and hope I clarify my question.

Regards,
Nathalia.

Posted by Community Admin on 25-Nov-2011 00:00
Hello Nathalia,

I'm glad that we managed to clarify the essence of the question. Creating new custom fields can be easily achieved through code. For instance, here's how you can add a new custom field to an Email Campaigns Subscriber:
//get an instance of MetadataManager and create a MetaType for Subscriber
var metaManager = Telerik.Sitefinity.Data.Metadata.MetadataManager.GetManager();
 metaManager.CreateMetaType(typeof(Subscriber));
 metaManager.SaveChanges();
 //create the new MetaField for the MetaType Subscriber, that will persist the "Organization" value
App.WorkWith().DynamicData().Type(typeof(Subscriber)).Field().CreateNew("Gender", typeof(String)).SaveChanges(true);
You can later operate with the custom field using the already discussed GetValue() and SetValue() extension methods:
var newslettersManager = NewslettersManager.GetManager(this.ProviderName);           
Subscriber subscriber = newslettersManager.GetSubscribers().Where(s => s.Email == email).SingleOrDefault();
            if(subscriber !=null && !String.IsNullOrEmpty(Gender.Text))
                
                    subscriber.SetValue("Gender", Gender.Text);
                    newslettersManager.SaveChanges();
                


All the best,
Boyan Barnev
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested 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 16-Feb-2012 00:00
I found the solution for the problem:

var property = Telerik.Sitefinity.Model.DataExtesion.GetValue(ImageModel,"property_name");

---------------------------------------------------------------------------------------------------------------------------------

Hi Boyan

I'm trying to get a custom field value from a Telerik.Sitefinity.Libraries.Model.Image but I don't see the GetValue and SetValue methods. I'm working with Sitefinity 4.4. Something I'm missing?

Thanks

This thread is closed