Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Olly Fray 11 posts 91 karma points
    May 01, 2019 @ 11:53
    Olly Fray
    0

    Getting IPublishedContent in Umbraco API Controller

    Hello!

    I've spent all morning going round in circles, trying to figure out how I can retrieve IPublishedContent properties.

    I know in Umbraco 8 I should now be able to do something like:

    node.Value<IPublishedContent>("propertyAlias");
    

    So I can then access that node's properties. But, when I try that in an API Controller, I am getting:

    The type or namespace name 'IPublishedContent' could not be found
    

    Any ideas why this might be? For reference full method here:

    [HttpGet]
    public IEnumerable<Game> SearchGames(string query = "", string id = "")
    {
        var gameList = new List<Game>();
    
        if (query.Length > 0 && id.Length > 0)
        {
            var root = Umbraco.Content(Int32.Parse(id));
            var games = root.Children.First(x => x.ContentType.Alias == "games");
            query = query.ToLower().Replace(" ", "-").Replace("'", "");
    
            foreach (var game in games.Children.Where(x => x.IsVisible()))
            {
                var gameName = game.Name.ToLower().Replace(" ", "-").Replace("'", "");
    
                var gameSelection = game.Value<IPublishedContent>("gameSelection");
    
                if (gameName.Contains(query))
                {    
                    var gameObj = new Game {
                        Name = game.Name,
                        Background = "",
                        Logo = "",
                        Url = game.Url, 
                        Global = ""
                    };
    
                    gameList.Add(gameObj);
                }
            }
        }
    
        return gameList;
    }
    

    Any help is much appreciated, my brain is melting!

  • Carlos Gomes 38 posts 184 karma points
    May 01, 2019 @ 14:04
    Carlos Gomes
    0

    Hi Olly,

    You need to include this namespace to be able to use IPublishedContent objects:

    using Umbraco.Core.Models.PublishedContent;

    Let me know if it solves the problem.

    Thanks,

  • Olly Fray 11 posts 91 karma points
    May 08, 2019 @ 08:07
    Olly Fray
    0

    Hi Carlos

    Thanks for your help! I had tried this already but it didn't seem to solve the issue.

    In fact a little while later it resolved itself, so I have no idea what the problem was!

  • Carlos Gomes 38 posts 184 karma points
    May 08, 2019 @ 10:44
    Carlos Gomes
    0

    Hi again Olly,

    You're welcome!

    That's weird... but it probably was something related to a binary that was not compiled or something

Please Sign in or register to post replies

Write your reply to:

Draft