How To: Work with Dynamic Sitefinity Content and Complex Properties in LINQ

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;

MGR: the Intelogist

About MGR: the Intelogist

SharePoint Server developer, turned Sitefinity developer, turned Angular developer, turned SharePoint Online consultant, turned Unily consultant, turned O365 consultant... Never a dull moment!

2 thoughts on “How To: Work with Dynamic Sitefinity Content and Complex Properties in LINQ

  1. good advice

    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.

    • MGR

      Glad it was of help to you =) Thanks for your kind words.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

This site uses Akismet to reduce spam. Learn how your comment data is processed.