Copied to clipboard

Flag this post as spam?

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


  • Aki 43 posts 214 karma points
    Sep 27, 2016 @ 08:41
    Aki
    0

    Update DocumentType Datatypes in code

    I'm trying to make a Function that Runs at Initialization of the Webpage and updates from a Backlog of DataTypes / Document Types

    This allows us to keep Dev and live Environment Synchronized.

    But i cant save Changes...

      var contentService = ApplicationContext.Current.Services.ContentTypeService;
                IContentType content = contentService.GetAllContentTypes().Where(x => x.Name == documentTypeName).First();
    
                DataTypeDatabaseType DT = DataTypeDatabaseType.Nvarchar;
    
                PropertyType Property = new Umbraco.Core.Models.PropertyType(PropertyName, DT, PropertyAlias);
    
                Property.Name = PropertyName;
    
                content.AddPropertyType(Property, Tab);
    
                contentService.Save(content, 1);
    

    The issues is the

    The INSERT statement conflicted with the FOREIGN KEY constraint "FKcmsPropertyTypecmsDataType_nodeId"

    I asume Its because it tryes to add the documentType again instead of updating ...

    I do have a working version but the code is Obsolete

    any help would be appreciated..

    PS. we have not yet updates and use Umbraco V 7.3.0

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Sep 27, 2016 @ 09:05
    Kevin Jump
    0

    Hi Aki,

    it looks like you are attempting to add the doctypes in a simliar way to how uSync does it, so it might be worth looking at the code in it's ContentTypeSerializer and ContentTypeBaseSerializer to give you pointers.

    Specifically when you are creating new properties on a content type you need to do checking to confirm they are not already there.

    uSync does that in its DeserializeProperties function. where it checks for both Key (guid) and Alias name - A t the very least you probably need to do the alias check

    property = content.PropertyTypes.SingleOrDefault(x => x.Alias == alias);
    

    I would then check this for null before doing any creates:

    With doctype compositions this is also a bit more complex because the property may exist on another composition further up and when you try to create it - umbraco will throw an error. this might be what's happening in your code. In uSync the CanCreateFunction does the checking on this.

    var allProperties = _allContentTypes.Where(x => x.ContentTypeComposition.Any(y => y.Id == item.Id)).Select(x => x.PropertyTypes);
    if (allProperties.Any(x => x.Any(y => y.Alias == alias)))                          
        canCreate = false;
    

    mainly what it is doing is checking to see if the doctype you are using is in used in any compositions and if the property type exists in them.

Please Sign in or register to post replies

Write your reply to:

Draft