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.
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....
}
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...
Or something like that.
My end goal is for a colleague to continue using their "regular" methods in their cshtml files, such as...
Thanks Dave
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
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
Try inheriting from "PublishedContentModel". Thats easier to implement as the actual interface.
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
But with no success as GetProperty returns a read only object.
Any help?
is working on a reply...