Dynamic Content (Module Builder) to XML
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;
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
Thanks! That was what I was thinking. But how do I get a list of categories with the name, not just the GUID ?
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;
// 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;
var customTaxonomy = mgr.GetTaxonomies<HierarchicalTaxonomy>().FirstOrDefault(t => t.Title ==
"My Custom Taxonomy"
);