Copied to clipboard

Flag this post as spam?

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


  • Chris Foot 61 posts 93 karma points
    Mar 30, 2016 @ 17:49
    Chris Foot
    0

    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:

      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!

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Mar 30, 2016 @ 21:41
    Alex Skrypnyk
    0

    Hi Chris,

    Try to use Automapper, create map from IPublishedContent to your class with one row. It's really simple.

    Thanks

  • Chris Foot 61 posts 93 karma points
    Mar 30, 2016 @ 21:57
    Chris Foot
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft