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

Forums / Developing with Sitefinity / Dynamic Content (Module Builder) to XML

Dynamic Content (Module Builder) to XML

4 posts, 0 answered
  1. Amanda Shafer
    Amanda Shafer avatar
    198 posts
    Registered:
    12 Oct 2012
    05 Aug 2013
    Link to this post
    Hi,
    I am trying to get a list of content items from the module builder into XML format. I would like it grouped by category.
    For example, 
    <vendors>
       <category type="Technology">
         <vendor>ABC Company</vendor>
         <vendor> XYZ Incorporated</vendor>
      </category>
     <category type="other">
        <vendor>GLS Co.</vencor>
    </category>
    </vendors>

    I'm trying to use the following code. However, I don't know how to get a list of categories. Some vendors fall into multiple categories. I would like it to appear numerous times if it's under more than one category.  Suggestions?

    protected void Page_Load(object sender, EventArgs e)
           {
               var Vendors = RetrieveCollectionOfVendors();
     
               XElement xml = new XElement("vendors",
                       from v in Vendors
                       orderby v.GetValue<string>("Title")
                        
                       select new XElement("category",
                                 new XAttribute("type", v.GetValue<TrackedList<Guid>>("Category").FirstOrDefault()),
                                 new XElement("firstName", v.GetValue<string>("Title")),
                                 new XElement("lastName", v.GetValue<string>("State")))
                       );
     
     
               // Saving to a file, you can also save to streams
               xml.Save(@"C:\vendors.xml");
           }
     
           public IQueryable<DynamicContent> RetrieveCollectionOfVendors(){   
               DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();           
               Type vendorType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.VendorResourceGuide.Vendor");           
               // Fetch a collection of "live" and "visible" vendor items.          
               var myCollection = dynamicModuleManager.GetDataItems(vendorType)               
                   .Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);                
               return myCollection;}  

  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    08 Aug 2013 in reply to Amanda Shafer
    Link to this post
    if you want to define your XML by category instead of group, you probably want to grab the list of categories first, then use the API to get items that match that category and append them.

    Alternatively, you could grab ALL the items, then filter them into say a Dictionary or Hashtable using the Category value as a key. Then you can iterate through those items, writing the XML in the format you need.

    I hope this makes sense and is helpful, please let me know if I've misunderstood the question
  3. Amanda Shafer
    Amanda Shafer avatar
    198 posts
    Registered:
    12 Oct 2012
    08 Aug 2013
    Link to this post
    Thanks! That was what I was thinking.  But how do I get a list of categories with the name, not just the GUID ?
  4. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    Registered:
    18 Jul 2012
    08 Aug 2013 in reply to Amanda Shafer
    Link to this post
    you can get this information from the TaxonomyManager by first getting the Category Taxonomy, then iterating through the taxon items in that taxonomy.

    Sitefinity creates default taxonomies for Categories (and Tags and Departments) so the Id is hard-coded in the manager. Here's a code sample that might help explain how it works:
    // get the taxonomy manager to interact with taxonomy data
    var mgr = TaxonomyManager.GetManager();
     
    // get the Categories taxonomy
    var categoriesTaxonomy = mgr.GetTaxonomy<HierarchicalTaxonomy>(TaxonomyManager.CategoriesTaxonomyId);
     
    // get the list of category taxon items from the Categories Taxonomy
    var categories = categoriesTaxonomy.Taxa;
     
    // read through each category taxon and get the title
    foreach (var category in categories)
    {
        var categoryTitle = category.Title;
    }

    If you go the other way around, and get the Category property from the Content item (which returns TrackedList<Guid> of taxonomy ids), you can again use the manager to get the category names:
    // Get list of category ids from content item field
    var categoryIDs = contentItem.GetValue<TrackedList<Guid>>("Category");
     
    // get the matching taxon from the manager by Id
    foreach (var categoryId in categories)
    {
        var categoryTaxon = mgr.GetTaxon<HierarchicalTaxon>(categoryId);
        var categoryTitle = categoryTaxon.Title;
    }

    If you are working with Tags instead, you can replace HierarchicalTaxon with FlatTaxon, and use the TaxonomyManager.TagsTaxonomyId property instead.

    You could even use this to retrieve custom taxonomy items by searching the manager by title instead of Id (this also works with "Categories" or "Tags" but I like to use the Id since it's hard-coded):
    var customTaxonomy = mgr.GetTaxonomies<HierarchicalTaxonomy>().FirstOrDefault(t => t.Title == "My Custom Taxonomy");

    Notice this time we use GetTaxonomies to retrieve them all, then filter to a matching item against your title. Once again the type depends on the Taxonomy type you're using (Hierarchical vs Flat).

    I hope this is helpful!
4 posts, 0 answered