Sorting Data - dynamicModuleManager.GetDataItems - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Sorting Data - dynamicModuleManager.GetDataItems

  • Sorting Data - dynamicModuleManager.GetDataItems
  • 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.
  • 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.
  • 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.
  • Hi Daniel, It still show me convert type error.  Any idea to solve the problem?  Thank you.
  • 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
  • Hi Daniel, I try your suggestion but it has error.  It show me "DynamicContent does not contain OrderBy" .  What wrong for my code?
  • Sorry Derick,

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

    Kind regards,
    Daniel
  • 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.
  • 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
  • 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
  • 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
  • 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!

  • 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
     
  • 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") ?