Copied to clipboard

Flag this post as spam?

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


  • keilo 568 posts 1023 karma points
    Jan 23, 2016 @ 07:46
    keilo
    0

    ContentService.GetContentOfContentType GetValue ArcheType

    I need some guidance with getting archetype values via the API (compiling a dll);

    I can run the following code in Razor and it works just fine.

    var inventRoot = Umbraco.Content(1100);
    foreach (var inventory in inventRoot.Descendants("course")){
    
    var entries = inventory.GetPropertyValue<Archetype.Models.ArchetypeModel>("propertyX");
    ..
    }
    

    Then I tried to implement it in the .CS (api usage to compile as dll) and realized
    .Descendants("course") was not working even though i have included using Umbraco.Web.

    It was giving error "IContent does not contain definition for 'Descendants' "

    So I have tried another approach (IContent vs IPublishedContent is not very clear to me yet) to iterate all the inventory items via

    ContentService.GetContentOfContentType(1081) where 1082 is the Id of the document type "course"

        using Umbraco.Core;
        using Umbraco.Web;
        using Archetype;
        using Archetype.Models;
        using Archetype.Extensions;
    
        var inventories = global::Umbraco.Core.ApplicationContext.Current.Services.ContentService.GetContentOfContentType(1081);
    
                    foreach (var inventory in inventories)
                    {
    
                        var entries = inventory.GetValue<Archetype.Models.ArchetypeModel>("propertyX");
                                      ^--here throws exception "The given key was not present in the dictionary."
                   }
    

    which seems to iterate all the "course" nodes but throws exception while reading the Archetype with GetValue

    Can someone shed light on how to read archetype property/values for all descendants of a node via the api properly?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jan 23, 2016 @ 11:16
    Marc Goodson
    0

    Hi Keilo

    The ContentService (which will return IContent objects) is basically for manipulating values in the backoffice database (eg. CRUD operations) rather than the fastest way to retrieve data to display on your published website.

    Your published website should use the UmbracoHelpers to query Umbraco Content, and these will return instances of IPublishedContent, from the Umbraco Cache rather than the database.

    You can still reference the Umbraco. helpers in a c# class library by instantiating the helper like so:

    var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
    
    var inventRoot = umbracoHelper.Content(1100);
    
Please Sign in or register to post replies

Write your reply to:

Draft