Copied to clipboard

Flag this post as spam?

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


  • Chris Gaskell 59 posts 142 karma points
    Feb 11, 2013 @ 09:39
    Chris Gaskell
    0

    V6 API IContentTypeService.RemoveContentType

    Hey,

    For a current project we really need to use the composition functionality to allow a doctype to inherit from multiple others. Although these emtods existin the API they arent wired in the V6 backoffice ad the changes will only come through with the new project Belle UI.

    Using a mixins type aproach I have wire dup the add composition method so one doc type can be 'composed' into another. This works great and changing the inherited doctype does push the changes onto the inheritor.

    So in summary this works a charm:

     

    .AddContentType(IContentTypeComposition contentType)

     

     

    However I'm struggling with remove functionality, it doesnt seem to work. I've also made sure there isnt any content on this doctype.

    From here http://our.umbraco.org/documentation/reference/management-v6/Models/ContentType :

     

    .RemoveContentType(string alias)

    Removes a ContentType with the supplied alias from the the list of composite ContentTypes.

    //Given a `ContentTypeService` object get a ContentType by its alias and 
    //remove the 'Meta' ContentType from its composition.
    var contentType = contentTypeService.GetContentType("textPage");
    bool success = contentType.RemoveContentType("meta");
    if(success)
        contentTypeService.Save(contentType);

     

    Anyone else tried this?

     

  • Chris Gaskell 59 posts 142 karma points
    Feb 11, 2013 @ 10:44
    Chris Gaskell
    0

    I think I've found a bug, reported on youtrack http://issues.umbraco.org/issue/U4-1690

  • Chris Gaskell 59 posts 142 karma points
    Feb 11, 2013 @ 11:11
    Chris Gaskell
    100

    For anyone else I'm working round it ATM with reflection

     

     if (! inheritor.ContentTypeCompositionExists(savingEntity.Alias))
    continue; // Doesnt inherit. Shouldnt be here.
     
    // Until http://issues.umbraco.org/issue/U4-1690 is resolved we'll need to reflect
    FieldInfo field = typeof(ContentTypeCompositionBase).GetField("_contentTypeComposition", BindingFlags.Instance | BindingFlags.NonPublic);
    var contentTypeCompositions = field.GetValue(inheritor) as List<IContentTypeComposition>;
    //Remove what we're looking for.
    contentTypeCompositions.Remove(contentTypeCompositions.First((x => x.Alias == savingEntity.Alias)));

            

    // Set value back
    field.SetValue(inheritor, contentTypeCompositions);
     
    // Invoke regular save 
    sender.Save(inheritor);
Please Sign in or register to post replies

Write your reply to:

Draft