👨‍💻
wiki - owain.codes
  • 💻 Welcome
  • Coding
    • Tips / How-to
      • Where is the hosts file on Windows?
      • Wrapped up a site
    • Snippets
      • Non-coding
        • TinyMCE config generator for Umbraco
        • Git Markdown
        • Command Prompt / Terminal
      • 👨‍💻Coding
        • Compile FrontEnd via NVM and Gulp
        • Node-Sass
        • SQL Statements
        • Build up SQL statements
        • Get current logged in user from Controller
        • Dynamic BlockList Label
        • Alt Tag
    • Umbraco
      • Luke - Version helper
      • Umbraco 10+
        • Find out Model of Current page
        • error NU1301: Unable to load the service index for source
      • Umbraco 9
        • Get the current page content type alias
      • Fluent SQL
      • Unable to open ConfigSource file
      • Delete items from backoffice by DocType ID
      • [WIP] Setting up Examine
      • Umbraco.ModelsBuilder assembly error
      • Working with IPublishedContent
      • How to Strongly Type to Models
      • Setting up a custom form
      • Getting bounced away from /umbraco
      • Examine
        • Explaining GroupedOr / GroupedAnd methods
      • Rosyln error
      • Models Builder Settings
      • Adding content to the backoffice
      • Pagination
      • Creating YYYY/MM folders
      • Configuration Error - CodeDom
    • C Sharp
      • Useful Links
      • Regex
      • Null check .any()
      • internal static and internal const
      • Dependency Injection
      • Dictionary<Tkey, TValue>
      • Linq / Lambda
  • Git
    • Git Actions
      • Build your own Git Action
      • Create a readme file automatically
    • Command line
      • Unstage a file
      • Ignore web.config changes on commit
      • Allow for case-insensitivity in Windows
      • Making VSCode your Git editor and diff tool
      • Add, Push, Pull, Clone
      • Make Terminal look nice
    • Conventional Commit
Powered by GitBook
On this page

Was this helpful?

  1. Coding
  2. Umbraco

Working with IPublishedContent

Umbraco.TypedContent

public ActionResult GetDistinctContentTypesForAuthor(string authorId)
        {
            //Create an empty list of type String
            List<string> allResults = new List<string>();

            // Setup what Search Index to use
            var searcher = ExamineManager.Instance.SearchProviderCollection["ContentHubSearcher"];

            // Tell the search what will be searched, this is content that will be searched
            ISearchCriteria searchCriteria = searcher.CreateSearchCriteria(IndexTypes.Content);

            
            // Create the lucene query, this is looking for all authors with the a matching authorId
            // within lucene, author is stored as an Key e.g. author: 9e0dad13195243389215ce452031ffb5
            
            IBooleanOperation query = searchCriteria.GroupedOr(new string[] { "author" }, authorId);

            // Run the search within Examine
            var queryResults = searcher.Search(query.Compile());

            // from the results, select all the Ids and save them in IEnumerable
            IEnumerable<int> nodeIds = queryResults.Select(x => x.Id);
            
            // Create a List of IPublishedContent using Umbraco.TypedContent and the IEnumberable nodeIds List
            List<IPublishedContent> publishedContent = Umbraco.TypedContent(nodeIds).ToList();

            
            // With the List of IPublishedContent - do what you need to do.
            if (publishedContent != null)
            {
                foreach (var item in publishedContent)
                {

                    if (item is GenericContent genericContent && genericContent.ContentLabel is GenericPageLabel pageLabel)
                    {
                        allResults.Add(pageLabel.LabelTitle.IfNullOrWhiteSpace(pageLabel.Name));
                    }
                    else if (item is VideoItem videoItem && videoItem.ContentLabel is GenericPageLabel videoPageLabel)
                    {
                        allResults.Add(videoPageLabel.LabelTitle.IfNullOrWhiteSpace(videoPageLabel.Name));
                    }
                    else if (item is WebinarContent webinarItem && webinarItem.ContentLabel is GenericPageLabel webinarPageLabel)
                    {
                        allResults.Add(webinarPageLabel.LabelTitle.IfNullOrWhiteSpace(webinarPageLabel.Name));
                    }
                    else if (item is StoryContent storyItem && storyItem.ContentLabel is GenericPageLabel storyPageLabel)
                    {
                        allResults.Add(storyPageLabel.LabelTitle.IfNullOrWhiteSpace(storyPageLabel.Name));
                    }
                    else
                    {
                        allResults.Add(Global.FriendlyFromDocTypeAlias(item.DocumentTypeAlias));
                    }
                }
            }


            return PartialView("Authors/ContentTypesDropdown", allResults.Distinct().OrderBy(x => x));


        }
PreviousUmbraco.ModelsBuilder assembly errorNextHow to Strongly Type to Models

Last updated 3 years ago

Was this helpful?