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
    Jul 09, 2019 @ 12:35
    Daniel Rogers
    0

    CS0029: Cannot implicitly convert type 'System.Guid' to 'Umbraco.Core.Udi'

    I are trying to auto create a Content Node as a sub node when ever a particular document type is added o the content.

    Example would be a content Node (sub node) every time a new page node is created.

    but I keep getting stuck with the parent node Id.

    an error simalar to below comes up

    CS0029: Cannot implicitly convert type 'System.Guid' to 'Umbraco.Core.Udi'

    on this line of code

    var newContent = cs.CreateContent("Components", key, "iBDComponentList"); 
    

    The error changes dependent on how or what you pass in place of "key"

    according to documentation I should be able to just use elem.Id.

    below is the context of the code around it.

            private void ContentService_Saving(IContentService cs, ContentSavingEventArgs e)
            {
                foreach (var elem in e.SavedEntities.Where(n => n.WasPropertyDirty("Id") &&
                     ((n.ContentType.Alias == "iBDHome" ||
                       n.ContentType.Alias == "iBDPage") &&
                       n.WasPropertyDirty("Id"))))
                {
                    Udi key = elem.Key;
    
                    var newContent = cs.CreateContent("Components", key, "iBDComponentList"); 
                    cs.Save(newContent);
                }
            }
    
  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Jul 09, 2019 @ 13:02
    Alex Skrypnyk
    0

    Hi Daniel

    elem.Key - is Guid, not Udi. Try this code:

    private void ContentService_Saving(IContentService cs, ContentSavingEventArgs e)
            {
                foreach (var elem in e.SavedEntities.Where(n => n.WasPropertyDirty("Id") &&
                     ((n.ContentType.Alias == "iBDHome" ||
                       n.ContentType.Alias == "iBDPage") &&
                       n.WasPropertyDirty("Id"))))
                {
                    Guid key = elem.Key;
    
                    var newContent = cs.CreateContent("Components", key, "iBDComponentList"); 
                    cs.Save(newContent);
                }
            }
    
  • Daniel Rogers 143 posts 742 karma points
    Jul 09, 2019 @ 13:19
    Daniel Rogers
    0

    Been there. But tried again. Copied your code direct. Gives this

    CS1503: Argument 3: cannot convert from 'System.Guid' to 'Umbraco.Core.Udi'

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Jul 09, 2019 @ 14:19
    Alex Skrypnyk
    0

    Daniel, what version of Umbraco are you using?

  • Daniel Rogers 143 posts 742 karma points
    Jul 09, 2019 @ 23:19
    Daniel Rogers
    0

    Umbraco 8.0.2 new fresh install

  • Daniel Rogers 143 posts 742 karma points
    Jul 12, 2019 @ 05:12
    Daniel Rogers
    100

    Have just updated to 8.1.

    Solved

    I need to be using ContentService_Saved as I wouldnt have a parent Id to link the sub document to.

    Using this allowed acces to the Udi

    private void ContentService_Saved(IContentService sender, ContentSavedEventArgs e)
        {
            foreach (var elem in e.SavedEntities.Where(n => n.WasDirty() &&
                 ((n.ContentType.Alias == "iBDHomePage" ||
                   n.ContentType.Alias == "iBDStandardPage"))))
            {
                var umbracoParentGuid = elem.GetUdi();
                var publishDate = DateTime.Now;
                var name = "Components";
                var docType = "iBDPageComponents";
    
    
                IContent content = sender.CreateContent(name, umbracoParentGuid, docType,-1);
                content.CreateDate = publishDate;
                content.UpdateDate = publishDate;
    
                sender.SaveAndPublish(content);
            }
        }
    
  • Enkosi 11 posts 86 karma points
    Dec 14, 2020 @ 12:33
    Enkosi
    1

    I'm just going to leave this for someone else who might find it useful. This applies to those who have or know the parent node (parentNode).

    To create a new node using CreateContent:

    var contentService = Current.Services.ContentService;
    var udi = new GuidUdi(parentNode.ContentType.ItemType.ToString(), parentNode.Key);
    var newNode = contentService.CreateContent(name, udi, contentTypeAlias);
    contentService.Save(newNode);
    

    or create a new node with the Create method using the Key attribute to create a GUID:

    var contentService = Current.Services.ContentService;
    var parentId = new Guid(parentNode.Key.ToString());
    var newNode = contentService.Create(name, parentId, contentTypeAlias);
    contentService.Save(newNode);
    

    I hope this helps you.

  • 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