Copied to clipboard

Flag this post as spam?

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


  • MB 108 posts 397 karma points
    Sep 15, 2018 @ 11:14
    MB
    0

    Get Content Items from Plug-In

    New to Umbraco and just completed my first plug-in, but now have been asked to so something a bit more advanced.

    I want to build a plug-in that can fetch and then list (in a grid) some details of the child items below the item it's placed on.

    I essence, I want to create similar functionality to enabling list-view mode on a document-type, but as a plug-in I can place on a document-type's property tab, so I don't need to disable the standard tree-view mode just to see a (possibly clickable) listing of an item's children.

    Basically, I'm looking for some pointers on how to fetch data on the child items below the current item, from a plug-in.

    Once I have the data, I would imagine the grid/angular stuff should be fairly straight forward.

  • Marc Goodson 1733 posts 11490 karma points MVP 7x c-trib
    Sep 15, 2018 @ 11:36
    Marc Goodson
    1

    Hi MB

    If you include the EntityService in your angular controller it has a GetChildren method that you can use to get the children of the current page you are:

    https://our.umbraco.com/apidocs/csharp/api/Umbraco.Core.Services.EntityService.html#UmbracoCoreServicesEntityServiceGetChildrenSystemInt32UmbracoCoreModelsUmbracoObjectTypes_

    To get the id of the current item being edited in the backoffice, you should add the editorState helper to your controller, and then you can use:

    editorState.current.id

    be aware that when a page is first created in Umbraco this won't have a value, but then nor will it have children! so just need to put a check in on whether the id is populated, before using the entityservice to look for children.

    regards

    Marc

  • MB 108 posts 397 karma points
    Sep 16, 2018 @ 23:09
    MB
    0

    Cheers.

    I'm not sure I understand how the C# EntityService API relates to the angular controller, but that gives me a starting point to investigate.

  • Nathan Woulfe 438 posts 1653 karma points MVP 5x c-trib
    Sep 17, 2018 @ 02:43
  • MB 108 posts 397 karma points
    Sep 17, 2018 @ 03:59
    MB
    0

    Thanks for that, a ui resource seems like it might meet the requirements.

    Would I be correct in assuming that getting the current content-node's ID for the property-editor's controller to use as its starting point, should be done using:

    https://our.umbraco.com/apidocs/ui/#/api/umbraco.services.editorState

    Just looking at the data that returns, my first guess would be that parentId of the property-editor's current state, refers to the content item it's sitting on, and that I can use that as the node to request child info on, using the method entiyResource.getChildren(parentId, type) - but I don't know it's that's a reliable value to use?

    Of course, I'll also need a reliable way to know if the property-editor is being displayed in Document or Document-Type mode... but one step at a time.

  • MB 108 posts 397 karma points
    Sep 17, 2018 @ 11:50
    MB
    0

    Managed to make progress and drilled down to the document, and started extracting property values.

    All was going well until I hit some properties with a value along the lines of: umb://document/abc12d3ef4567890e1f2345g678hi901

    What do I need to use now to get the actual value ?

  • Marc Goodson 1733 posts 11490 karma points MVP 7x c-trib
    Sep 17, 2018 @ 19:45
    Marc Goodson
    0

    Hi MB

    Sorry for the confusion totally pasted the wrong link, and as Nathan points out, completely meant entityResource.

    If you are now looping through the properties and finding a value in the format:

    umb://document/abc12d3ef4567890e1f2345g678hi901

    then this is the new Umbraco UDI a unique identifier for a content item (to eventually replace integer ids everywhere), my guess is the property that has this value is a Content Picker, this property type, allows editors to pick other content nodes that are related to the current content item... so you'd need to make a further request to retreive this picked objects details if you need to display any of it's details in your listview.

    regards

    Marc

  • MB 108 posts 397 karma points
    Sep 17, 2018 @ 22:57
    MB
    0

    Yes, it's one of those xxxx2 content pickers. Actually, there are quite a few properties I need to access values for, with .config.idType = "udi"

    I have to admit to being a bit stuck at the moment. I can't seem to find a BackOffice Resource, Service or Helper that addresses this, and am not sure where to go from here.

  • Nathan Woulfe 438 posts 1653 karma points MVP 5x c-trib
    Sep 17, 2018 @ 23:35
    Nathan Woulfe
    100

    Sorry my first reply didn't expand on anything, this one will be better...

    To fetch a single content item by id/udi, you need the contentResource. You can inject that into your controller, and access its methods, like so:

    (() => {
        function myCtrl(contentResource) {
            contentResource.getById(id)
                .then(resp => { // do something with the response });
        }
    
        angular.module('umbraco').controller('myCtrl', ['contentResource', ctrl]);    
    })();
    

    Full docs live here => https://our.umbraco.com/apidocs/ui/#/api/umbraco.resources.contentResource

  • MB 108 posts 397 karma points
    Sep 18, 2018 @ 01:04
    MB
    0

    Ah-Hah!

    If property.config.idType === "uid" then use property.value as an ID to fetch the associated content.

    That was the missing piece of the puzzle for me, and now I have it working - although I doubt debugged.

    Thanks to all for the help.

Please Sign in or register to post replies

Write your reply to:

Draft