Copied to clipboard

Flag this post as spam?

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


  • pbl_dk 150 posts 551 karma points
    Mar 01, 2024 @ 14:20
    pbl_dk
    0

    Cannot convert from 'System.Guid' to 'Umbraco.Cms.Core.Udi'

    When I use a surfacecontroller and the documentation here for content creation: https://docs.umbraco.com/umbraco-cms/reference/management/services/contentservice/create-content-programmatically

    // Create a variable for the GUID of the parent page - Catalogue, where you want to add a child item.
    var parentId = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
    
    // Create a new child item of type 'Product'
    var demoproduct = ContentService.Create("Microphone", parentId, "product");
    

    I get the error: CS1503 Argument 3: cannot convert from 'System.Guid' to 'Umbraco.Cms.Core.Udi' on the parentId.

    Anybody know why this error comes?

    I am using Umbraco 13.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 01, 2024 @ 20:56
    Jeavon Leopold
    100

    Are you injecting ContentService into your controller?

    Something like:

        public class MyController : SurfaceController
    {
        private readonly IContentService _contentService;
        public MyController(
            IUmbracoContextAccessor umbracoContextAccessor,
            IUmbracoDatabaseFactory databaseFactory,
            ServiceContext services,
            AppCaches appCaches,
            IProfilingLogger profilingLogger,
            IPublishedUrlProvider publishedUrlProvider,
            IContentService contentService)
            : base(umbracoContextAccessor, databaseFactory, services, appCaches, profilingLogger, publishedUrlProvider)
        {
            _contentService = contentService;
        }
    
        public IActionResult Index()
        {
            var parentId = Guid.Parse("b6fbbb31-a77f-4f9c-85f7-2dc4835c7f31");
    
            var demoproduct = _contentService.Create("Microphone", parentId, "product");
            return Content("Hello world");
        }
    }
    

    Also ensure the Guid exists in your content and not just this sample one?

  • pbl_dk 150 posts 551 karma points
    Mar 01, 2024 @ 22:27
    pbl_dk
    0

    Ah, great, I see the error. I was using _contentService.CreateContent - which specifically asked for a Umbraco UID.

    Your code doesn't do that it seems. When I use

    _contentService.Create, it works superb.

    I don't know why there are two different methods there. I guess there is a reason somewhere.

    Thanks :-)

Please Sign in or register to post replies

Write your reply to:

Draft