I’m trying to get a collection of DynamicContent items in Sitefinity. For example, if I’m trying to get the published and visible Case Studies (where, Case Study is a custom module in Sitefinity) I can use the following:
IQueryable<DynamicContent> myCollection;
var providerName = String.Empty;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
myCollection = dynamicModuleManager.GetDataItems(caseStudyType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderByDescending(c => c.PublicationDate);
But, what if I want to filter this myCollection further more. What if I want to filter against one of the complex fields. For example, images are saved as ContentLinks in a ContentLink array. Similarly, Categories (or, any categorisation) can be retrieved as guids in a List<Guid>. You can use the Contains() method of the list to filter against the Guid.
What you need to do first is to get the Guid of the Category you want to filter against. Sample method to get the GUID:
protected Guid CategoryGuid(string categoryTitle)
{
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
var taxonomyManager = TaxonomyManager.GetManager();
var taxon = taxonomyManager.GetTaxa<HierarchicalTaxon>().Where(t => t.Title == categoryTitle).FirstOrDefault(); //get the guid of the category with the specific Title
Guid categoryGuid = taxon.Id;
return categoryGuid;
}
Now that you have the category Id, you need to alter the aforementioned LINQ statement to incorporate the new filter.
selectedCategoryGuid = CategoryGuid(_categoryFilter); //where _categoryFilter will be a string representation of the category name that you want to filter your collection against
myCollection = dynamicModuleManager.GetDataItems(caseStudyType).Where(i => i.GetValue<IList<Guid>>(“Category”).Contains(selectedCategoryGuid) && i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderByDescending(c => c.PublicationDate);
A bit silly really, but don’t forget to add a using for Linq:
using System.Linq;
Hello it’s me, I am also visiting this web page daily, this site is genuinely good and the people are actually sharing fastidious thoughts.
Glad it was of help to you =) Thanks for your kind words.