Copied to clipboard

Flag this post as spam?

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


  • David Burrows 4 posts 74 karma points
    Feb 04, 2020 @ 12:51
    David Burrows
    0

    Return an IEnumerable<IPublishedContent> from External API

    Hi all,

    I'm trying to return a IEnumerable from an External API. Or basically create in C# an enumerable list of NewsItems.

    The API calling and deserialization of the JSON in to an an array of Objects is done. What I need to do next is to turn the array of Objects in to a IEnumerable variable.

    In pseudo-code I think what I need is...

    IList<NewsItem> nodes = new List<NewsItem>();
    foreach( item in apiArrayOfItems )
    {
       NewsItem newsItem = new NewsItem();
       newsItem.setProperty( "title", item["title"] );
       newsItem.setProperty( "summary", item["summary"] );
       newsItem.setProperty( "content", item["content"] );
       nodes.add( newsItem );
    }
    return nodes;
    

    Or something like that.

    My end goal is for a colleague to continue using their "regular" methods in their cshtml files, such as...

    if (nodes.Any())
    {
        <ul class="news-items main-content">
            @foreach ( var news in nodes )
            {
                <div class="latest-news-item">
                    <div class="row">
                        <div class="col-sm-9">
                            @if (news.HasValue("Title"))
                            {
                                <h4 style="display:inline-block"><a href="@news.Url" title="@news.Name">@news.GetPropertyValue("Title")</a></h4><span class="news-date" style="float:right">(@news.CreateDate.ToString("dd") @news.CreateDate.ToString("MMM") @news.CreateDate.ToString("yyyy"))</span>
                            }
    
    
                            @if (news.HasValue("Summary"))
                            {
                                <p>@news.GetPropertyValue("Summary")</p>
                            }
    
    
                            <a href="@news.Url" class="btn btn-nhs"><span class="text">Read More</span></a>
                        </div>
                    </div>
                </div>
            }
        </ul>
    }
    

    Thanks Dave

  • David Brendel 786 posts 2949 karma points c-trib
    Feb 04, 2020 @ 17:32
    David Brendel
    0

    Hi David,

    do you have a class of NewsItem in your project? As in theory that code should work like that.

    What ist the actual issue you have?

    Regards David

  • David Burrows 4 posts 74 karma points
    Feb 04, 2020 @ 19:24
    David Burrows
    0

    In a word “No”. I think I’m a bit further along and I’m going to try creating the class and inheriting from IPublishedContent and a few other classes.

    My main problem is that I’m a C++ and other stuff programmer and don’t have much experience with .net, C#, etc, etc

  • David Brendel 786 posts 2949 karma points c-trib
    Feb 04, 2020 @ 21:19
    David Brendel
    0

    Try inheriting from "PublishedContentModel". Thats easier to implement as the actual interface.

  • David Burrows 4 posts 74 karma points
    Feb 06, 2020 @ 15:03
    David Burrows
    0

    Hi,

    I sorted out the import from Json in to a PublishedContentModel by creating a classes and using the with the [JsonProperty("XXXX")] method. This worked very well and imports the data.

    My challenge now is that the Umbraco Properties helper methods (HasProperty, GetPropertyValue, etc, etc) don't work. I think this is because the "values" aren't stored as properties their stored as variables within the class. I can't add them to the Umbraco property variable as my code only has read access to this.

    I've tried changing the definition of, in the below example "Summary", to have a get and set using the property variable

    public string Summary { 
    get { return this.GetPropertyValue<string>("header"); }
    set { return this.GetProperty.Value("header", value); }
     }
    

    But with no success as GetProperty returns a read only object.

    Any help?

            [PublishedContentModel("newsItem")]
            public partial class newsItem : PublishedContentModel
            {
        #pragma warning disable 0109 // new is redundant
                public new const string ModelTypeAlias = "newsItem";
                public new const PublishedItemType ModelItemType = PublishedItemType.Content;
        #pragma warning restore 0109
    
                public NewsItem(IPublishedContent content)
                    : base(content) {}
    
        #pragma warning disable 0109 // new is redundant
                public new static PublishedContentType GetModelContentType()
                {
                    return PublishedContentType.Get(ModelItemType, ModelTypeAlias);
                }
        #pragma warning restore 0109
    
                public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<NewsItem, TValue>> selector)
                {
                    return PublishedContentModelUtility.GetModelPropertyType(GetModelContentType(), selector);
                }
    
                [ImplementPropertyType("Summary")]
                [JsonProperty("description")]
                public string Summary { get; set; }
    
                // More properties....
            }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies