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 41 posts 213 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 5378 posts 19869 karma points MVP 3x 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 41 posts 213 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 5378 posts 19869 karma points MVP 3x admin c-trib
    Jul 09, 2019 @ 14:19
    Alex Skrypnyk
    0

    Daniel, what version of Umbraco are you using?

  • Daniel Rogers 41 posts 213 karma points
    Jul 09, 2019 @ 23:19
    Daniel Rogers
    0

    Umbraco 8.0.2 new fresh install

  • Daniel Rogers 41 posts 213 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);
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft