Copied to clipboard

Flag this post as spam?

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


  • Matt Salmon 22 posts 43 karma points
    Jul 30, 2010 @ 18:46
    Matt Salmon
    1

    Creating a document type property programatically

    Hi all,

    I posted in this forum a little while ago about the options available to developers to work with the Umbraco back-end programmatically. Since then I've been cracking on with using the API and have successfully created a document type using this snippet:

    umbraco.BusinessLogic.User user = new umbraco.BusinessLogic.User("admin");
    DocumentType dt = DocumentType.MakeNew(user, "Test Template");
    dt.Description = "Test template description";
    dt.Alias = "testTemplate";
    dt.Save();

    I then went on to flesh out my document type with a new property (as you would in the web interface by going to "Generic Properties" and creating a new property manually. This was less straightforward as I couldn't find any good examples, and the API documentation for the PropertyType class usefully says "Summary description for propertytype"! But I've managed to piece together something that seems to work in order to create a boolean property on my new document type:

    dt.AddPropertyType(new umbraco.cms.businesslogic.datatype.DataTypeDefinition(-49), "bOptIn", "Opt In");
    dt.Save();

    The thing I'm wondering now is how would I add this property to a custom tab. For example, I know also that I can create a custom tab on the document type using:

    dt.AddVirtualTab("My custom tab");
    dt.Save();

    But it's specifying that the new property I'm creating is to belong to this new tab that's causing me bother. Can anyone fill me in on how to do this?

  • Matt Brailsford 3343 posts 17706 karma points MVP 8x c-trib
    Jul 30, 2010 @ 22:23
    Matt Brailsford
    1

    Hi Matt,

    I think you should be able to do the following:

    int tabId = dt.AddVirtualTab("My Custom Tab");
    dt
    .AddPropertyType(DataTypeDefinition(-49), "bOptIn", "Opt In");
    dt
    .SetTabOnPropertyType(dt.getPropertyType("bOptIn"), tabId);
    dt
    .Save();

    Matt

  • Matt Salmon 22 posts 43 karma points
    Jul 31, 2010 @ 00:19
    Matt Salmon
    0

    That's done it, thanks Matt.

  • donatas 2 posts 22 karma points
    May 23, 2012 @ 18:56
    donatas
    0

    How did you guys manage to get it working? I keep getting:

    Method not found: 'Void umbraco.cms.businesslogic.ContentType.AddPropertyType(umbraco.cms.businesslogic.datatype.DataTypeDefinition, System.String, System.String)'.

    I'm using Umbraco 4.7.1.

  • rob 75 posts 170 karma points
    Jul 13, 2012 @ 12:01
    rob
    0

    @donatas the method no longer returns null

     In the older version of Umbraco the method's return type was public void AddPropertyType whereas the new one public PropertyType AddPropertyType

    So the final code(assuming correct assemblies are referenced):

    var dt =DocumentType.GetByAlias("TestDocType");
    var pType = dt.AddPropertyType(newDataTypeDefinition(-49),"testprop","test prop");
    the above was shamelessly copied from stackoverflow 
  • donatas 2 posts 22 karma points
    Jul 14, 2012 @ 11:53
    donatas
    0

    Thanks Rob, in the end I got it working. BTW that's my post on stackoverflow :)

  • Jonathon Cove 10 posts 81 karma points
    1 week ago
    Jonathon Cove
    0

    In case anyone stumbles on this question and is using Umbraco 8, I have written how to create custom properties for members here https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/78210-add-custom-property-to-membership-member#comment-335629

Please Sign in or register to post replies

Write your reply to:

Draft