Need a little help querying custom modules

Posted by Community Admin on 04-Aug-2018 19:23

Need a little help querying custom modules

All Replies

Posted by Community Admin on 07-Mar-2012 00:00

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

Posted by Community Admin on 07-Mar-2012 00:00

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();

Posted by Community Admin on 07-Mar-2012 00:00

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);

Posted by Community Admin on 07-Mar-2012 00:00

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

Posted by Community Admin on 07-Mar-2012 00:00

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.


If your filter type is a string, you will likely need to escape the "userName". (not sure if that is needed for GUID. More about that in this thread.)

No promises on this one. I'm still working through this issue myself.

Posted by Community Admin on 24-Mar-2012 00:00

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);

The column "is_location_published" is a custom boolean field in my custom Dynamic Module. So, if you use the GetValue<T>(fieldName) function, where T is the type of the field and fieldName is the name of your column, then you can filter on your data and let Sitefinity handle constructing the correct SQL.

Hope this helps,
Bobby

This thread is closed