1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Need a little help querying custom modules

Need a little help querying custom modules

6 posts, 0 answered
  1. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    07 Mar 2012
    Link to this post

    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

  2. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    07 Mar 2012
    Link to this post
    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();

  3. David
    David avatar
    99 posts
    Registered:
    06 Sep 2012
    07 Mar 2012
    Link to this post
    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);

  4. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    07 Mar 2012
    Link to this post
    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
    }

  5. Dan Sorensen
    Dan Sorensen avatar
    256 posts
    Registered:
    13 Aug 2010
    07 Mar 2012
    Link to this post
    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.
  6. Bobby
    Bobby avatar
    8 posts
    Registered:
    04 Feb 2012
    24 Mar 2012
    Link to this post
    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
6 posts, 0 answered