Copied to clipboard

Flag this post as spam?

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


  • Daniel Rogers 143 posts 742 karma points
    Sep 29, 2023 @ 03:32
    Daniel Rogers
    0

    programmatically create content not working

    This seems to be an old issue that I are having trouble with.

    Asking again as all the posts are for old version of umbraco.

    I'm running v12.2.0 )Latest version.

    Have followed this documentation https://docs.umbraco.com/umbraco-cms/reference/management/services/contentservice/create-content-programmatically witch was last updated 16hrs ago at time of writing this post.

    The only difference I are running the code from server side in a hangfire scheduled task.

    1. I get the parent document

      IPublishedContent catFolder = query.ContentAtRoot().DescendantsOrSelfOfType("IBDExypnosLandingPage").FirstOrDefault().FirstChildOfType("IBDExypnosCategoriesFolder");

    2. create the IContent

      // assign parent id. Guid parentId = catFolder.Key;

      // create content var newCatagory = _contentService.Create(Name, parentId, "IBDExypnosCategoriesPage");

    Up until here everything is working fine. 1st issue

    newCatagory.SetValue("onPageTitle", "hello");
    newCatagory.SetValue("titleAndDescription", jsonConvert.SerializeObject(seo));
    

    these line generate this error: Variation null,null is not supported by the property type.

    have also tried:

    newCatagory.SetValue(propertyTypeAlias: "onPageTitle", "hello", segment: null);

    2nd issue

    _contentService.SaveAndPublish(newCatagory);
    

    this line generate this error: Cannot save content with an empty name.

    Anybody with some ideas

  • Billy 55 posts 266 karma points c-trib
    Sep 29, 2023 @ 10:55
    Billy
    0

    Are you sure that the 'Name' variable is not empty here?

    var newCatagory = _contentService.Create(Name, parentId, "IBDExypnosCategoriesPage");
    

    If the document type is variable by culture don't forget to add the name for each culture.

    newCatagory.SetCultureName("english name", "en-us");  newCatagory.SetCultureName("dutch name", "nl");
    
  • roli81 7 posts 58 karma points
    Oct 20, 2023 @ 08:50
    roli81
    0

    I have the exact same problem on 12.2.0. Have you found a solution yet?

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 20, 2023 @ 09:40
    Huw Reddick
    0

    I think you need to pass a udi not id for the parent

        var parent = _contentService.GetById(model.ParentId);
        var forum = _contentService.CreateContent(model.Title, parent.GetUdi(), "forum");
    
  • roli81 7 posts 58 karma points
    Oct 20, 2023 @ 10:04
    roli81
    0

    Hi Hew thanks for your quick reply. But I have solved my problem in the meantime. 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);
        }
    }
    
  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Oct 20, 2023 @ 10:23
    Huw Reddick
    0

    That was going to be my next suggestion :D

                post = _contentService.Create(postName, parent, "forumPost");
                if (post.AvailableCultures.Any())
                {
                    foreach (var language in languages)
                    {
                        post.SetCultureName(postName,language.IsoCode);
                    }
                }
    
  • 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.

Please Sign in or register to post replies