Copied to clipboard

Flag this post as spam?

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


  • roli81 7 posts 58 karma points
    Oct 20, 2023 @ 08:23
    roli81
    0

    Cannot create content programmatically

    Hello everyone

    I'm trying to create content programmatically. I had already done this in previous versions (9 & 10), now I use Umbraco 12.2.0. Now when I call ContentService.CreateAndSave() I get an error that the content cannot be saved with an empty name. However, I pass the following string “roland-schenk” to the method. Are there any new restrictions on the names of a node?

    public void CreatePage(IMember member, TeacherProfile profile)
    {
        var itemName =  $"{member.Name}".Replace(" ", "-");
    
        IPublishedContent? parent = null;
    
        if (ContextAccessor.TryGetUmbracoContext(out var ctx))
        {
            parent = ctx.Content.GetAtRoot().FirstOrDefault(c => c.ContentType.Alias == Constants.DocumentTypes.MsHomePage.Alias)?
                .Children.FirstOrDefault(c => c.ContentType.Alias == Constants.DocumentTypes.MsSchoolOverview.Alias);
        }
    
    
    
        if (parent != null)
        {
            var content = _contentService.CreateAndSave(itemName.ToLowerInvariant(), parent.Id, Constants.DocumentTypes.MsMusicTeacherPage.Alias);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Name, member.Name);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.InstrumentLocations, JsonConvert.SerializeObject(GetInstrumentLocationBlockList(profile.InstrumentLocations)));
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Website, JsonConvert.SerializeObject(new Link()
            {
                Url = profile.Website,
                Type = LinkType.External
            }));
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.TeachingType, profile.Subtitle);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Email, member.Email);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Lon, profile.Lon.ToString());
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Lat, profile.Lat.ToString());
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Address, profile.Address);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.City, profile.City);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.ZipCode, profile.ZipCode);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.State, profile.State);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Style, profile.Style);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.LessonsForAge, profile.LessonsFrom);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Member, JsonConvert.SerializeObject(member));
            _contentService.SaveAndPublish(content);
        } 
    }
    

    System.InvalidOperationException: Cannot save content with an empty name. at Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement.DocumentRepository.EnsureInvariantNameExists(IContent content) at Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement.DocumentRepository.SanitizeNames(IContent content, Boolean publishing) at Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement.DocumentRepository.PersistNewItem(IContent entity) at Umbraco.Cms.Core.Cache.DefaultRepositoryCachePolicy2.Create(TEntity entity, Action1 persistNew) at Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement.EntityRepositoryBase2.Save(TEntity entity) at Umbraco.Cms.Core.Services.ContentService.Save(IContent content, Nullable1 userId, ContentScheduleCollection contentSchedule) at Umbraco.Cms.Core.Services.ContentService.CreateAndSave(String name, Int32 parentId, String contentTypeAlias, Int32 userId) at Sss.Mutobo.Core.Services.UserProfileService.CreatePage(IMember member, TeacherProfile profile) in E:_sss\musigschuel.ch\src\Sss.Mutobo.Core\Services\UserProfileService.cs:line 47 at Sss.Mutobo.Core.Controllers.Api.MemberController.SaveTeacherProfile(TeacherProfile profile) in E:_sss\musigschuel.ch\src\Sss.Mutobo.Core\Controllers\Api\MemberController.cs:line 131 at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(ActionContext actionContext, IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.

  • roli81 7 posts 58 karma points
    Oct 20, 2023 @ 10:01
    roli81
    100

    I was able to solve my problem. It was because it is a multilanguage solution and I have to make sure that all language versions exist. Yes, we Swiss people have a hard time with our languages ;-).

    public void CreatePage(IMember member, TeacherProfile profile)
    {
        var itemName = $"{profile.Name}";
    
        IPublishedContent? parent = null;
    
        if (ContextAccessor.TryGetUmbracoContext(out var ctx))
        {
            parent = ctx.Content.GetAtRoot().FirstOrDefault(c => c.ContentType.Alias == Constants.DocumentTypes.MsHomePage.Alias)?
                .Children.FirstOrDefault(c => c.ContentType.Alias == Constants.DocumentTypes.MsSchoolOverview.Alias);
        }
    
    
    
        if (parent != null)
        {
            var content = _contentService.Create(itemName.ToLowerInvariant(), parent.Id, Constants.DocumentTypes.MsMusicTeacherPage.Alias);
    
            foreach (var lang in _localizationService.GetAllLanguages())
            {
                content.SetCultureName(itemName, lang.CultureInfo.Name);
                content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.TeachingType, profile.Subtitle, lang.CultureInfo.Name, string.Empty);
                content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.State, profile.State, lang.CultureInfo.Name, string.Empty);
                content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Style, profile.Style, lang.CultureInfo.Name, string.Empty);
                content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.InstrumentLocations, JsonConvert.SerializeObject(GetInstrumentLocationBlockList(profile.InstrumentLocations)), lang.CultureInfo.Name, string.Empty);
            }
    
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Website, JsonConvert.SerializeObject(new List<Link>() {
                new Link()
            {
                Url = profile.Website,
                Type = LinkType.External
            }
            }));
    
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.LessonsForAge, profile.LessonsFrom);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Name, member.Name);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Email, member.Email);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Lon, profile.Lon.ToString());
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Lat, profile.Lat.ToString());
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Address, profile.Address);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.City, profile.City);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.ZipCode, profile.ZipCode);
            content.SetValue(Constants.DocumentTypes.MsMusicTeacherPage.Fields.Member, member.Id);
            _contentService.SaveAndPublish(content);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft