Need a little help querying custom modules
The below code was auto generated by Sitefinity. How do I iterate through each object in myCollection?
// Demonstrates how a collection of Orders can be retrieved
public void RetrieveCollectionOfOrders()
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type orderType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.RioGrandeFishingTournamentOrders.Order");
CreateOrderItem(dynamicModuleManager, orderType);
// This is how we get the collection of Order items
var myCollection = dynamicModuleManager.GetDataItems(orderType);
// At this point myCollection contains the items from type orderType
The below code was auto generated by Sitefinity. I added 2 parameters to the function, one for the Username the other a password. Note that they are Guid's, not strings. How do I modify the code below to filter by username & password?
// Demonstrates how Order content items can be retrieved through filtering
public void RetrieveOrderThroughFiltering(Guid userName, Guid password)
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type orderType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.RioGrandeFishingTournamentOrders.Order");
CreateOrderItem(dynamicModuleManager, orderType);
// This is how we get the order items through filtering
var myFilteredCollection = dynamicModuleManager.GetDataItems(orderType).Where("UrlName = \"Some UrlName\"");
// At this point myFilteredCollection contains the items that match the lambda expression passed to the Where extension method
// If you want only the first matching element you can freely get it by ".First()" extension method like this:
// var myFirstFilteredItem = myFilteredCollection.First();
xxxxxxxxx
Anybody know why the filter is being ignored in the following code snipped? All of the orders are being bound to the Grdiview, not just the one with the matching Username. Username is a custom field of type Guid that was added to the module.
Guid userName = new Guid("85e6f69a-f9bc-4ba7-a7c1-6a15857c2f12");
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type orderType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.RioGrandeFishingTournamentOrders.Order");
var myFilteredCollection = dynamicModuleManager.GetDataItems(orderType).Where("Username = " + userName);
GridView1.DataSource = myFilteredCollection;
GridView1.DataBind();
Is it possible to filter a module by a custom field? I tried it a different way but still no success. Again, it's returning all orders.
var myFilteredCollection = dynamicModuleManager.GetDataItems(orderType).Where(i => i.GetValue<
Guid
>("Username") == userName);
Use a foreach to loop over items (type DynamicContent) your myCollection object.
foreach
(var item
in
myCollection)
// get something out of the DynamicContent item using the GetValue() accessor method.
string
foo = item.GetValue(
"foo"
).ToString();
string
bar = item.GetValue(
"bar"
).ToString();
// do something here
Regarding the ignored filter, here are a couple things to try:
You might be getting back duplicates as I discovered. Sitefinity keeps multiple copies of each in their various states. See more about it in this thread.
Hi David,
This is the code I have been using to filter custom Dynamica module fields.
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type businessType = TypeResolutionService.ResolveType(BusinessContentType);
Type businessLocationType = TypeResolutionService.ResolveType(BusinessLocationContentType);
var businesses = dynamicModuleManager.GetDataItems(businessType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible == true && i.GetValue<
bool
>("is_location_published") == false);