Filter Dynamic Module content by Category (HierarchicalTaxon)
Hi,
We have used the new Module Builder in Sitefinity 4.4 to make a module that stores Contacts for a business website. Each contact has an associated Category (HierarchicalTaxon) which we have used to associate a location with the contact.
The Module Builder provides code examples for fetching and filtering a Contact by name etc. What we would like to do is filter a Contact by Category (HierarchicalTaxon). I have the Category GUID stored in a cookie.
1.
HttpCookie LocationCategoryCookie = Request.Cookies[
"location_category"
];
2.
location_category = Server.HtmlEncode(LocationCategoryCookie.Value);
3.
4.
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
5.
Type contactType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Contacts.Contact"
);
6.
var myFilteredCollection = dynamicModuleManager.GetDataItems(contactType).Where(
"The code we need is here... get contact by location_category"
);
var myFilteredCollection = dynamicModuleManager.GetDataItems(contactType).
Where(item => item.getValue<TrackedList<Guid>>("Category").Contains(category.Id));
Thanks for the response, this was similar to my first attempt, but the following errors appear...
Error 1 Cannot convert lambda expression to type 'string' because it is not a delegate type
Error 2 Delegate 'System.Func<
Telerik.Sitefinity.DynamicModules.Model.DynamicContent
,int,bool>' does not take 1 arguments
Error 3 'Telerik.Sitefinity.DynamicModules.Model.DynamicContent' does not contain a definition for 'getValue' and no extension method 'getValue' accepting a first argument of type 'Telerik.Sitefinity.DynamicModules.Model.DynamicContent' could be found (are you missing a using directive or an assembly reference?)
Error 4 The name 'category' does not exist in the current context
anyone?
ok, tried a different method today to no avail...
01.
protected
void
Page_Load(
object
sender, EventArgs e)
02.
RetrieveContactThroughFiltering();
03.
04.
05.
public
void
RetrieveContactThroughFiltering()
06.
07.
HttpCookie LocationCategoryCookie = Request.Cookies[
"location_category"
];
08.
System.Guid location_category =
new
System.Guid(LocationCategoryCookie.Value);
09.
OpenAccessDynamicModuleProvider dynamicModuleManager =
new
OpenAccessDynamicModuleProvider();
10.
11.
var taxonomyManager = TaxonomyManager.GetManager();
12.
var taxonGuidId =
new
Guid(LocationCategoryCookie.Value);
13.
ITaxon taxon = taxonomyManager.GetTaxon(taxonGuidId);
14.
15.
string
itemTypeName =
"Telerik.Sitefinity.DynamicTypes.Model.OurContacts.Contact"
;
16.
Type itemType = TypeResolutionService.ResolveType(itemTypeName);
17.
var manager = ManagerBase.GetMappedManager(itemType,
""
);
18.
19.
GetItems(taxon, dynamicModuleManager, itemType);
20.
21.
22.
23.
private
TaxonomyPropertyDescriptor GetPropertyDescriptor(Type itemType, ITaxon taxon)
24.
return
TaxonomyManager.GetPropertyDescriptor(itemType, taxon);
25.
26.
27.
private
IEnumerable GetItems(ITaxon taxon, OpenAccessDynamicModuleProvider contentProvider, Type itemType)
28.
TaxonomyPropertyDescriptor prop = GetPropertyDescriptor(itemType, taxon);
29.
int
? totalCount = 1;
30.
var items = contentProvider.GetItemsByTaxon(taxon.Id, prop.MetaField.IsSingleTaxon, prop.Name, itemType,
string
.Empty,
string
.Empty, 0, 100,
ref
totalCount);
31.
return
items;
32.
|
Hi Christian,
If your dynamic module contains a metafield of type classification (e.g. Category) you can obtain its value using our extension method GetValue(). Here's a sample how to retrieve all published dynamic module items that have the "Cat1" category assigned to them:
var manager = TaxonomyManager.GetManager();
//Get the GUID of the desired category
var myCategoryId = manager.GetTaxa<HierarchicalTaxon>().Where(t => t.Name ==
"Cat1"
).SingleOrDefault().Id;
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type dynamicmoduleitemType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.MyDynamicModule.Dynamicmoduleitem"
);
var myItems = dynamicModuleManager.GetDataItems(dynamicmoduleitemType)
.Where(d => d.GetValue<TrackedList<Guid>>(
"Category"
).Contains(myCategoryId)
&& d.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
Works a treat :)
This is our first Sitefinity site, the help and info is much appreciated.
i had the same problem, i needed to add this to my code:
using System.Linq;
Hello,
Thank you for sharing your solution with the community.
Regards,
Stefani Tacheva
Telerik
Hi,
Thanks for sharing this, I wanted do a filter and it worked fine.
Filter List based on Category
Hi all,
I have a different problem. The solution given is to fetch all dynamic items that has a particular category. I need to query the dynamic items that falls in a list of categories.
the pseudo code will be like
var productWithCats = dm.GetDataItems(type).Where(d => d.Visible == true && d.Status == ContentLifecycleStatus.Live && d.GetValue<IList<Guid>>("Category").Contains(selectedCategories));
where selectedCategories is an array of Guid.
Is there any way to achieve this?
Regards
Ajai