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);
}
}
}
}
}
}
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?
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:
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
Hi Jonathan,
Yes, that is it, great job, i didn't know it mattered. You saved me again :D
BR
Josip
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.