Copied to clipboard

Flag this post as spam?

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


  • john blair 48 posts 219 karma points
    Sep 05, 2019 @ 18:30
    john blair
    0

    How to call CreateContent given an IContent node which has no udi

    I am trying to convert some code from v7 to v8. In the content saving event I want to create a child node onto the content that is being saved so:

    IContent parent;  // This is being saved.
    IContent newContent = contentService.CreateContent(rule.NodeName, parent.Id, rule.DocTypeAliasToCreate);
    

    The trouble is IContent ony has Id which is an int - I need a Udi and have no idea how to convert it to a Udi. The v7 of umbraco would accept a node id but not any more. How do I get the Udi for a IContent.Id ? IContent has a guid key but equally i have no idea how to get from that to a Udi? Thanks.

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Sep 06, 2019 @ 04:07
    Shaishav Karnani from digitallymedia.com
    0

    Hi John,

    parent.Id int works fine in Umbraco 8.

    So, this line will work perfectly for you. IContent newContent = contentService.CreateContent(rule.NodeName, parent.Id, rule.DocTypeAliasToCreate);

    Please try and let us know if you have any issues.

    Regards,

    Shaishav

  • john blair 48 posts 219 karma points
    Sep 06, 2019 @ 09:03
    john blair
    0
    namespace Umbraco.Core.Services
    

    {

    public static class ContentServiceExtensions
    {
        /
        public static IContent CreateContent(this IContentService contentService, string name, Udi parentId, string contentTypeAlias, int userId = -1);
    

    Thanks for the reply - but it does not work per my earlier post. I have pasted in the definition of CreateContent and you will see the [arentId parameter is a Udi - not an int. This is taken from Assembly Umbraco.Core, version 8.0.0.0.

  • john blair 48 posts 219 karma points
    Sep 06, 2019 @ 09:10
    john blair
    100

    Hi Shaishav,

    You forced me to look again. We both mentioned the wrong method CreateContent. There is indeed a CreateAndSave method that takes an int parentId

    IContent CreateAndSave(string name, int parentId, string contentTypeAlias, int userId = -1);
    

    I can work with that! Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft