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
    Mar 22, 2010 @ 10:05
    Chris Gaskell
    0

    Update doctype via API

    Hi,

    I have a requirement to add one tab and two fields to each document type in my CMS (there are about 40). Is there anyway I can do this with the API?

    Thanks,

    Chris

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 22, 2010 @ 10:41
    Lee Kelleher
    2

    Hi Chris,

    It's a bit rough-n-ready, here's a quick code sample:

    using umbraco.cms.businesslogic.datatype;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.propertytype;
    DataTypeDefinition dataType1 = new DataTypeDefinition(1234);
    // DataTypeDefinition dataType2 = new DataTypeDefinition(5678);
    
    List<DocumentType> docTypes = DocumentType.GetAllAsList();
    
    foreach (DocumentType docType in docTypes)
    {
        int tab = docType.AddVirtualTab("New Tab");
    
        docType.AddPropertyType(dataType1, "New_Field_1", "New Field 1");
        docType.AddPropertyType(dataType1, "New_Field_2", "New Field 1");
    
        PropertyType pt1 = docType.getPropertyType("New_Field_1");
        PropertyType pt2 = docType.getPropertyType("New_Field_2");
    
        docType.SetTabOnPropertyType(pt1, tab);
        docType.SetTabOnPropertyType(pt2, tab);
    
        docType.Save();
    }

    First get the data-types for the properties (fields) that you want to add.  Then get a list of all the document-types and loop through them.

    Add the new property-types to the document-type.  Now here's the PITA, the "AddPropertyType" is void, so doesn't return the new property-type, so we need to fetch it back from the database.  Once you've got the new property-types, then you can set which tab they belong to.

    Save the document-type to trigger any associated events/hooks. (You can remove this if you like?)

    I haven't tested this in any way... so use at your own risk! ;-)

    Good luck, Lee.

  • Chris Gaskell 59 posts 142 karma points
    Mar 22, 2010 @ 10:42
    Chris Gaskell
    0

    Cheers Lee, I'll let you know how I get on.

    Chris

  • Chris Gaskell 59 posts 142 karma points
    Mar 23, 2010 @ 10:15
    Chris Gaskell
    1

    Cheers Lee, that worked fine.

    In the end though I didnt add the properties for each doctype - I created a maste doctype and moved all the current doctypes below it.

    Thanks for your help mate.

    Chris

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Mar 23, 2010 @ 10:37
    Lee Kelleher
    0

    Hi Chris,

    You know, I was going to suggest that, (creating a new master doc-type) - but wasn't 100% of any repercussions of doing that! :-)

    Glad it worked ... might do the same on a couple of my client's v3->v4 upgraded installs.

    Cheers, Lee.

  • James Hildeman 92 posts 88 karma points
    Sep 28, 2010 @ 22:05
    James Hildeman
    0

    In your code sample, how did you decide on 1234 in the following?  I need to add a new True/False and was digging through the database to see if I could find a pk (id) that represents True/False.  What am I missing?

    DataTypeDefinition dataType1 = new DataTypeDefinition(1234);

     

Please Sign in or register to post replies

Write your reply to:

Draft