Copied to clipboard

Flag this post as spam?

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


  • Josip 131 posts 522 karma points c-trib
    1 week ago
    Josip
    0

    EnsureUmbracoContext is null

    I want to create content node (if dont alredy exist) every time member is saved.

    For that I need Umbraco helper to check if node alredy exist and content services to create that content node.

    But when i click save I am getting null reference on EnsureUmbracoContext

    This is my code:

    public class CreateFolders : IUserComposer
    {
        public void Compose(Composition composition)
        {
            // Append our component to the collection of Components
            // It will be the last one to be run
            composition.Components().Append<SubscribeToContentServiceSavingComponent>();
        }
        public class SubscribeToContentServiceSavingComponent : IComponent
        {
            // access to the ContentService by injection
            private readonly IContentService _contentService;
            private readonly IUmbracoContextFactory _context;
            public SubscribeToContentServiceSavingComponent(IContentService contentService)
            {
                _contentService = contentService;
            }
            public SubscribeToContentServiceSavingComponent(IUmbracoContextFactory context)
            {
                _context = context;
            }
            // initialize: runs once when Umbraco starts
            public void Initialize()
            {
                MemberService.Saving += MemberService_Saving;
            }
            // terminate: runs once when Umbraco stops
            public void Terminate()
            {
            }
            private void MemberService_Saving(IMemberService sender, SaveEventArgs<IMember> e)
            {
                foreach (IMember member in e.SavedEntities)
                {
                    //var cref = _context.EnsureUmbracoContext();
                    using (var cref = _context.EnsureUmbracoContext())
                    {
                        var cache = cref.UmbracoContext.Content;
                        bool hasExistingFolder = cache.GetById(2198).ChildrenOfType("downloadsFile").Any(x => x.Value("authorPicker") == member);
    
                        if (!hasExistingFolder)
                        {
                            var createFolder = _contentService.Create(member.Id.ToString(), 2198, "downloadsFile");
    
                            // Convert member key to udi so we can add member to member picker
                            var memberUdi = Udi.Create(Constants.UdiEntityType.Document, member.Key);
                            createFolder.SetValue("authorPicker", memberUdi);
    
                            // Save and publish file
                            _contentService.SaveAndPublish(createFolder);
                        }
                    }
                }
            }
        }
    }
    
  • Jonathan Distenfeld 77 posts 432 karma points
    1 week ago
    Jonathan Distenfeld
    100

    Hi Josip,

    this is just a guess, since I did not use the Components before, but in your class SubscribeToContentServiceSavingComponent you have two constructor. In the first one, you dont assign your _context field to any value, so if you have an instance of this class being created via this constructor _context will be null. Have you tried injecting both, IContentService and IUmbracoContextFactory in the same constructor?

    Found something similar here: https://our.umbraco.com/forum/umbraco-8/96652-constructor-injection-in-an-icomponent-implementation#comment-305582

    ~ Jonathan

  • Josip 131 posts 522 karma points c-trib
    1 week ago
    Josip
    1

    Hi Jonathan,

    Yes, that is it, great job, i didn't know it mattered. You saved me again :D

    BR

    Josip

Please Sign in or register to post replies

Write your reply to:

Draft