Sorting Data - dynamicModuleManager.GetDataItems

Posted by Community Admin on 04-Aug-2018 14:36

Sorting Data - dynamicModuleManager.GetDataItems

All Replies

Posted by Community Admin on 22-Jul-2013 00:00
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.
Posted by Community Admin on 22-Jul-2013 00:00
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.
Posted by Community Admin on 22-Jul-2013 00:00
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
Posted by Community Admin on 22-Jul-2013 00:00
Hi Daniel, I try your suggestion but it has error.  It show me "DynamicContent does not contain OrderBy" .  What wrong for my code?
Posted by Community Admin on 22-Jul-2013 00:00
Sorry Derick,

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

Kind regards,
Daniel
Posted by Community Admin on 22-Jul-2013 00:00
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.
Posted by Community Admin on 22-Jul-2013 00:00
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
Posted by Community Admin on 22-Jul-2013 00:00
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.
Posted by Community Admin on 22-Jul-2013 00:00
Hi Daniel, It still show me convert type error.  Any idea to solve the problem?  Thank you.
Posted by Community Admin on 23-Jul-2013 00:00
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
Posted by Community Admin on 26-Jul-2013 00:00
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
Posted by Community Admin on 09-Jan-2015 00:00

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!

Posted by Community Admin on 09-Jan-2015 00:00

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

Posted by Community Admin on 09-Jan-2015 00:00
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
 
Posted by Community Admin on 09-Jan-2015 00:00

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

Posted by Community Admin on 09-Jan-2015 00:00
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
 
Posted by Community Admin on 28-Apr-2017 00:00

Above Daniel Plomp Code  will working fine for me when i am doing sorting for string  field like Name, Title,...

When we required sorting the number value(s) having string datatype then above code wont help. need to do work around to get the proper list value below is my working sample code.

public IQueryable<DynamicContent> RetrieveCollectionEducationDetails(string OrderBY)
   
    var providerName = String.Empty;
   DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);
            Type servicesType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Education.Education");
       if (OrderBY == "Most Liked")
       
       //Get Complete List
       var myCollection = dynamicModuleManager.GetDataItems(servicesType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderByDescending(i => i.GetValue<string>("LikeCount"));
       //return myCollection;
 
       //var myCollection1 = dynamicModuleManager.GetDataItems(servicesType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderByDescending(o => o.GetValue<decimal?>("LikeCount"));
        
var dictionary = new Dictionary<DynamicContent, int>();
foreach (var temp in myCollection)
  
dictionary.Add(temp, int.Parse(temp.GetValue("LikeCount").ToString()));
 
List<DynamicContent> list = new List<DynamicContent>();
foreach (KeyValuePair<DynamicContent, int> author in dictionary.OrderByDescending(key => key.Value))
list.Add(author.Key);
return list.AsQueryable();
else if (OrderBY == "Most Recent")
var myCollection = dynamicModuleManager.GetDataItems(servicesType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true).OrderByDescending(i => i.GetValue<DateTime>("LastModified"));
return myCollection;
else
  var myCollection = dynamicModuleManager.GetDataItems(servicesType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible == true);
  return myCollection;
  
  
 
 
This thread is closed