I am currently writing an api for a custom application my company is writing. Part of this involves getting published content out in JSON format. When I try serializing ipublished content I obviously get a lot of umbraco data and relations that I simply don't need (in fact it actually fails with a stack overflow!). Is there a way to get just the custom properties from an item of content without specifying the fields?
I am using webapi and passing it objects to serialize itself and I'm using a dynamic to manually specify the fields. The Product type which I'm initially selecting into is from modelsbuilder. My code currently looks a little like this:
public object Get(string keywords = "")
{
// Get Data from Umbraco
var allProducts = Umbraco.TypedContent(1100).Children.Select(x => new Product(x));
if (keywords != "")
{
allProducts = allProducts.Where(x => x.Name.Contains(keywords));
}
return allProducts.Select(x => new
{
id = x.Id,
name = x.Name,
price = x.Price
});
}
It seems to me that there should be a simple way to do this and i'm probably going about the whole thing entirely the wrong way but I just can't seem to find it!
Wouldn't that require me recreating a class every time a field on the document type changed? The point is to dynamically output any custom fields on the document without having to recompile the project.
Serializing published content
Hi Everyone,
I am currently writing an api for a custom application my company is writing. Part of this involves getting published content out in JSON format. When I try serializing ipublished content I obviously get a lot of umbraco data and relations that I simply don't need (in fact it actually fails with a stack overflow!). Is there a way to get just the custom properties from an item of content without specifying the fields?
I am using webapi and passing it objects to serialize itself and I'm using a dynamic to manually specify the fields. The Product type which I'm initially selecting into is from modelsbuilder. My code currently looks a little like this:
It seems to me that there should be a simple way to do this and i'm probably going about the whole thing entirely the wrong way but I just can't seem to find it!
Hi Chris,
Try to use Automapper, create map from IPublishedContent to your class with one row. It's really simple.
Thanks
Hi Alex,
Wouldn't that require me recreating a class every time a field on the document type changed? The point is to dynamically output any custom fields on the document without having to recompile the project.
Thanks
Chris
is working on a reply...