ContentLink ChildItem Sorting

Posted by Community Admin on 04-Aug-2018 03:51

ContentLink ChildItem Sorting

All Replies

Posted by Community Admin on 23-Jul-2013 00:00

I has a code below which retrieve from database.  The data has a chidItem.  I need to sort the child item before it assign to the other variable.  Anyone has idea how I can make it?  Please help!

 ContentLink[] contentLink = (ContentLink[])channelScheduleManagement.GetValue("DownloadableFiles");
                               
                scheduleExcelTitle = new string[contentLink.Length];
                scheduleExcelURL = new string[contentLink.Length];
                for (int c = 0; c < contentLink.Length; c++)
               
                    var doc = libraryManager.GetDocument(contentLink[c].ChildItemId);
                    scheduleExcelTitle[c] = doc.Title;
                    scheduleExcelURL[c] = doc.Url;                    
               

Posted by Community Admin on 23-Jul-2013 00:00

Hi Derick,

I created some code for you to check out. This is what it does:

  1. Gets a single Press Release item
  2. Get the Documents from the Press Release item
  3. Orders the results by Title descending
  4. Adds the Title and MediaUrl to a Dictionary<string, string>

Be aware that when you sort Documents by Title you need to use the Title.Value property, since the Title property is of type LString, which is a Sitefinity specific string used for multilingual purposes.

If you want the Title to be sorted for a specific Culture, you need to use the method GetString on the Title property.

/// <summary>
        /// Get Press Releases
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmdGet_OnClick(object sender, EventArgs e)
 
            var results = GetDocumentsFromPressRelease("Press Release 1");
 
        
 
        /// <summary>
        /// Get Documents from a Press Release item
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        private Dictionary<string, string> GetDocumentsFromPressRelease(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 pressRelease = dynamicModuleManager.GetDataItems(pressReleaseType).Where(x =>
                x.Status == ContentLifecycleStatus.Live &&
                x.Visible &&
                x.GetValue<string>("Title") == title).OrderBy(x => x.GetValue<string>("Title")).FirstOrDefault();
 
 
            if (pressRelease == null) return null;
 
            // Get Documents from the Press Release
            var documents = pressRelease.GetValue<ContentLink[]>("Documents");
 
            // Define result Dictionary
            var results = documents.Select(item => GetDocument(item.ChildItemId))
                .Where(document => document != null).OrderByDescending(x => x.Title.Value)
                .ToDictionary<Document, string, string>(document => document.Title, document => document.MediaUrl);
 
            return results;
        
 
        /// <summary>
        /// Get a Document from the Libraries
        /// </summary>
        /// <param name="documentId"></param>
        /// <returns></returns>
        private Document GetDocument(Guid documentId)
            var manager = LibrariesManager.GetManager();
            return (documentId != Guid.Empty) ? manager.GetDocument(documentId) : null;
        


Kind regards,
Daniel

Posted by Community Admin on 25-Jul-2013 00:00

Hi,

Thank you Danie for sharing this solution. I hope that Derick will find it useful.

Regards,
Stefani Tacheva
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

This thread is closed