Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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
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);
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);
// Save and publish file
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
Yes, that is it, great job, i didn't know it mattered. You saved me again :D
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted