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

Forums / Developing with Sitefinity / Sorting Data - dynamicModuleManager.GetDataItems

Sorting Data - dynamicModuleManager.GetDataItems

16 posts, 0 answered
  1. Derick
    Derick avatar
    6 posts
    Registered:
    13 Jul 2013
    22 Jul 2013
    Link to this post
    I has a code below to retrieve data

    Type dType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Schedule.ScheduleManagement");

                // This is how we get the collection of event items
                var myCollection = dynamicModuleManager.GetDataItems(dType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible && i.GetValue<string>("Title").ToString() == channel + " Schedule").FirstOrDefault();
                // At this point myCollection contains the items from the the type

                return myCollection;

    May I know it is possible to sorting the data when it is retrieve from database?  I found there is OrderBy function but i cant apply it.  Any one has an idea, please help.
    Thank you.
  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    22 Jul 2013
    Link to this post
    Sorry, can't post to this forum, since it gives all kind of 500 and 404 errors.
    Just make sure you are using the System.Linq namespace.
  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    22 Jul 2013 in reply to Derick
    Link to this post
    Hi Derick,

    Can't post a normal post to this forum, unfortunately.
    Since you are using the FirstOrDefault method, there isn't much to sort anyway, but if you don't call ToList, First, Single etc, your collection still is an IQueryable, so that means you can do the OrderBy.

    Kind regards,
    Daniel
  4. Derick
    Derick avatar
    6 posts
    Registered:
    13 Jul 2013
    22 Jul 2013 in reply to Daniel Plomp
    Link to this post
    Hi Daniel, I try your suggestion but it has error.  It show me "DynamicContent does not contain OrderBy" .  What wrong for my code?
  5. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    22 Jul 2013 in reply to Derick
    Link to this post
    Sorry Derick,

    I edited my previous post, since posting to the forum is not working correctly.
    (invalid content)

    Kind regards,
    Daniel
  6. Derick
    Derick avatar
    6 posts
    Registered:
    13 Jul 2013
    22 Jul 2013 in reply to Daniel Plomp
    Link to this post
    Hi Daniel, thanks for the reply.  If I remove the FirstOrDefault, I can use the orderby in my return value?
    I has try to remove it and add the OrderBy in my return value return MyCollection.OrderBy(x => x.Title), The x => x.Title give me an error.  Sorry about that because I'm still new in Sitefinity.
  7. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    22 Jul 2013 in reply to Derick
    Link to this post
    Hi Derick,

    Yeah, you should also do the OrderBy like this:
    var results = myCollection.OrderBy(x=>x.GetValue<string>("Title"));

    Since, 'Title' is not a property on the DynamicContent object.

    Kind regards,
    Daniel
  8. Derick
    Derick avatar
    6 posts
    Registered:
    13 Jul 2013
    22 Jul 2013
    Link to this post
    Hi Daniel,
        Sorry again is me.  It still show me an error
    Cannot implicit convert type System.Linq......to Telerik.Sitefinity.DynamicMo.......
    Below is the function 
         public static DynamicContent RetrieveCollectionOfChannelScheduleManagement()
    {
         Type dType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Schedule.ScheduleManagement");

                // This is how we get the collection of event items
                var myCollection = dynamicModuleManager.GetDataItems(dType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible && i.GetValue<string>("Title").ToString() == channel + " Schedule").FirstOrDefault();
                // At this point myCollection contains the items from the the type
        
          var result = myCollection.OrderBy( i => i.GetValue<string>("Title"));
          
    return result;
                //return myCollection
    }
    The return result has error.  Any idea?
    Thank you very much.
  9. Derick
    Derick avatar
    6 posts
    Registered:
    13 Jul 2013
    22 Jul 2013
    Link to this post
    Hi Daniel, It still show me convert type error.  Any idea to solve the problem?  Thank you.
  10. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    23 Jul 2013 in reply to Derick
    Link to this post
    Hi Derick,

    If you first do a FirstOrDefault call, it isn't obvious to sort the collection, since it only contains 1 item. So maybe I don't get it, but try to look at this code to see if that works for you?

    /// <summary>
            /// Get Press Releases
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void cmdGet_OnClick(object sender, EventArgs e) {
     
                // Either get one result or a collection by using FirstOrDefault
                var results = GetPressReleases("Press Release 1").FirstOrDefault();
            }
     
            /// <summary>
            /// Get Press Releases by Title
            /// </summary>
            /// <param name="title"></param>
            /// <returns></returns>
            public IQueryable<DynamicContent> GetPressReleases(string title) {
     
                // Get the DynamicModule manager
                var dynamicModuleManager = DynamicModuleManager.GetManager();
                var pressReleaseType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Pressreleases.PressRelease");
     
                // Get a collection with Press Releases
                var pressReleases = dynamicModuleManager.GetDataItems(pressReleaseType).Where(x =>
                    x.Status == ContentLifecycleStatus.Live &&
                    x.Visible &&
                    x.GetValue<string>("Title") == title).OrderBy(x => x.GetValue<string>("Title"));
     
                return pressReleases;
            }

    Kind regards,
    Daniel
  11. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    26 Jul 2013
    Link to this post
    Hello Derick,

    Daniel is correct. There is no point to call OrderBy after you have called FirstOrDefault. The FirstOrDefault extension method will give you the first item of the collection or null if the collection is empty.

    So remake your query by removing the FirstOrDefault at the end:

    var myCollection = dynamicModuleManager.GetDataItems(dType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible && i.GetValue<string>("Title").ToString() == channel + " Schedule").FirstOrDefault(); //remove this last

    And modify it as Daniel's code:
    var myCollection = dynamicModuleManager.GetDataItems(dType).Where(i => i.Status == ContentLifecycleStatus.Live && i.Visible && i.GetValue<string>("Title").ToString() == channel + " Schedule").OrderBy(x => x.GetValue<string>("Title"));

    Regards,

    Pavel Benov
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  12. Nummo
    Nummo avatar
    6 posts
    Registered:
    16 Jan 2013
    09 Jan 2015
    Link to this post

    Hi,

    Is it possible to define a sorting by custom field of dynamic content in RadListView control?

    I've defined a custom content in Module Builder with two additional fields Title and Description. How can I define sort in RadListView control by Title field? The following code of widget template for this content produces an exception with message "Invalid property or field - 'Title' for type: DynamicContent".

    <telerik:RadListView ID="dynamicContentListView" ItemPlaceholderID="ItemsContainer" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false" >
        <SortExpressions>
            <telerik:RadListViewSortExpression FieldName="Title" SortOrder="Ascending" />
        </SortExpressions>
        <LayoutTemplate>
            <ul class="spravochnik-simptomov-list">
                <asp:PlaceHolder ID="ItemsContainer" runat="server" />
            </ul>
        </LayoutTemplate>
        <ItemTemplate>
            <li  data-sf-provider='<%# Eval("Provider.Name")%>' data-sf-id='<%# Eval("Id")%>' data-sf-type="Telerik.Sitefinity.DynamicModules.Model.DynamicContent">
                <sf:DetailsViewHyperLink ID="DetailsViewHyperLink" TextDataField="Title" runat="server" data-sf-field="Title" data-sf-ftype="ShortText" />
            </li>
        </ItemTemplate>
    </telerik:RadListView>

    Many thanks for advices!

  13. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    09 Jan 2015 in reply to Nummo
    Link to this post

    Hi Nummo,

    You should set the sorting from the Widget Properties itself.

    Just fill in the SortExpression field in the widget advanced settings (see screenshot). This can be found within the Advanced Settings ->  ControlDefinition > Views > DynamicContentMasterView

    Best,
    Daniel

  14. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    09 Jan 2015
    Link to this post
    Hello,

    You can also check the following Documentation article:

    http://docs.sitefinity.com/filtering-and-sorting-the-items

    Regards,
    Pavel Benov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
  15. Nummo
    Nummo avatar
    6 posts
    Registered:
    16 Jan 2013
    09 Jan 2015
    Link to this post

    Thanks, Daniel!

    Thanks, Pavel!

    Pavel, can I use filter expression language to define custom sort order? Where I can find complete reference for filter expression language described on following link?

    http://docs.sitefinity.com/filter-expressions-for-content-items#filter-by-category

    What means {Taxonomy ID} in expression Visible = true AND Status = Live AND Category.Contains("{Taxonomy ID}") ?

  16. Pavel Benov
    Pavel Benov avatar
    341 posts
    Registered:
    14 Mar 2016
    09 Jan 2015
    Link to this post
    Hi Nummo,

    The syntax of FilterExpressions is SQL based. You can check the following forum thread:

    http://www.sitefinity.com/developer-network/forums/developing-with-sitefinity/filterexpression

    {Taxonomy ID} refers to the GUID value of the Id of a Taxonomy which should replace the curly brackets (and its contents) from the samples. You can obtain Taxonomy Id's by dropping Categories widget on a page and then going to its Advanced settings and removing the GUID value from the TaxonomyId field. The widget will display all taxonomies and their IDs afterwards. Please check the following article:

    http://docs.sitefinity.com/categories-widget

    Check the "Configuring Categories widget to display custom classification" section.

    Regards,
    Pavel Benov
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
     
16 posts, 0 answered