Copied to clipboard

Flag this post as spam?

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


  • Ault Nathanielsz 87 posts 407 karma points c-trib
    May 03, 2021 @ 10:21
    Ault Nathanielsz
    1

    Get content by ID from within EditorModelEventManager

    I'm having a go at setting a default value for a property on creation using this example: https://our.umbraco.com/documentation/reference/events/EditorModel-Events/#usage

    However, I need to get the new node's parent name. I can get the ID easily enough e.Model.ParentID but I clearly need to call in some other services as I cannot call IPublishedContent or similar at this point to easily get the node name for that ID.

    I'm sure it's relatively simple, but my GoogleFu is failing me.

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    May 03, 2021 @ 10:50
    Bjarne Fyrstenborg
    100

    Hi..

    You can inject the services you need access to in the constructor, e.g.

    private readonly IUmbracoContextAccessor _umbracoContextAccessor;
    
    public SubscribeToEditorModelEvents(IUmbracoContextAccessor umbracoContextAccessor)
    {
        _umbracoContextAccessor = umbracoContextAccessor;
    }
    

    and then using it like this:

    var cache = _umbracoContextAccessor.UmbracoContext.Content;
    var parent = cache.GetById(e.Model.ParentId);
    

    I recently updated the documentation with an additional example using SendingMemberModel and injecting `IMemberGroupService. https://our.umbraco.com/documentation/reference/events/EditorModel-Events/#usage

    /Bjarne

  • Ault Nathanielsz 87 posts 407 karma points c-trib
    May 03, 2021 @ 15:47
    Ault Nathanielsz
    0

    Thank You! Exactly what I was after

Please Sign in or register to post replies

Write your reply to:

Draft