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

Forums / Developing with Sitefinity / Exception assigning dynamic content type to a taxonomy category programmatically

Exception assigning dynamic content type to a taxonomy category programmatically

3 posts, 1 answered
  1. Uttam
    Uttam avatar
    21 posts
    Registered:
    17 Mar 2015
    02 Jun 2015
    Link to this post

    Hi,

    Using Sitefinity version 8.0+ I'm getting exception "The specified property 'Category' does not exist on type 'Telerik.Sitefinity.DynamicTypes.Model.ModelofType.ModelOfType'" while assigning dynamic content type to a taxonomy category and here is my code:

     

    //Create new Dynamic Item                
       DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();     
                        Type contentType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.ModelofType.ModelOfType");
                        DynamicContent contentItem = dynamicModuleManager.CreateDataItem(contentType);
                       
                        contentItem.SetValue("Title", title);
                        
                        var urlName = Regex.Replace(title, @"[\\~#%&*{}/:<>?|""@ ]", "-").ToLower();
                        contentItem.SetString("UrlName", urlName);

                        contentItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
                        contentItem.SetValue("PublicationDate", DateTime.Now);
                        contentItem.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Draft");                    
                        
                        //assign content to category
    //Get the Categories taxonomy
                        var categoryTaxonomy = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().SingleOrDefault(s => s.Name == "Custom Categories");
    var subCategoryTaxa = categoryTaxonomy.Taxa.Where(t => t.Title.ToString().ToLower() == "some-sub-category".ToLower()).FirstOrDefault();
                       
                        contentItem.Organizer.AddTaxa("Category", subCategoryTaxa.Id);

                        // Save the changes
                        dynamicModuleManager.SaveChanges();

                        // Send the Item for approval
                        var bag = new Dictionary<string, string>();
                        bag.Add("ContentType", contentItem.GetType().FullName);
                        WorkflowManager.MessageWorkflow(contentItem.Id, contentItem.GetType(), dynamicModuleManager.Provider.ToString(), "Publish", false, bag);

     

    Any input is appreciated.

    Thanks!!! 

     

     ​ 

  2. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    08 Dec 2016
    05 Jun 2015
    Link to this post
    Hello Uttam,

    Regarding the error message which you are getting: "The specified property 'Category' does not exist on type 'Telerik.Sitefinity.DynamicTypes.Model.ModelofType.ModelOfType'" it seems that there is not a custom field Category added to your dynamic module. Can you please make sure that you have a category custom field added to your dynamic module?

    In addition to this, please note that in order to add categories and tags you can use the extension method SetValue().

    myModuleItem.SetValue("CustomFieldName", "The value");

    Please make sure that you have added the following namespace:

    using Telerik.Sitefinity.Model;

    Here is also a sample code which add a certain category titled Cat1 to a selected event item:

    EventsManager eventsManager = new EventsManager();
      
                // Gets the evnt item
                var eventItem = eventsManager.GetEvents().Where(ev => ev.Title == "Event UI").FirstOrDefault();
      
                // Gets the manager which operates with Taxonomies
                TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
      
                // Get the ID of the singe instance (HierarchicalTaxon) of type Hierarchicaltaxonomy
                //(This can be Categories or any custom HierarchicalTaxonomy)
                var categoryTaxonomy = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().SingleOrDefault(s => s.Name == "Categories");
                var singleCategoryId = categoryTaxonomy.Taxa.Where(c => c.Title == "Cat1").FirstOrDefault().Id;
      
                  
                // Get a collection of all the IDs of the categories of the event, if any
                var myEventsCategories = eventItem.GetValue<TrackedList<Guid>>("Category");
      
                // add the selected category id to the above collection
                myEventsCategories.Add(singleCategoryId);
      
                // assign the modified collection to the Category metafield of the event item
                // this is equivalent to adding a new Category to a event through the UI
                eventItem.SetValue("Category", myEventsCategories);
      
                eventsManager.SaveChanges();

    You may also checkout the video for your convenience demonstrating the results on your side.

    You can use the same approach for adding categories to your dynamic module items. You can also checkout the following forum thread and the following forum post where the same has been discussed.

    Regards,
    Sabrie Nedzhip
    Telerik
     
    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 Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
    Answered
  3. Uttam
    Uttam avatar
    21 posts
    Registered:
    17 Mar 2015
    05 Jun 2015 in reply to Sabrie Nedzhip
    Link to this post
    Thanks Sabrie, your input helped a lot.
3 posts, 1 answered