Copied to clipboard

Flag this post as spam?

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


  • MuirisOG 382 posts 1284 karma points
    Feb 10, 2015 @ 18:24
    MuirisOG
    0

    Looping through new Property Types

    Is there a way in code to loop through new Property Types (which I've added to a Document Type) and assign them to a new tab.
    They seem to have gone under the "Generic" tab, and I've tried doing a For Each on getVirtualTabs but it isn't finding that generic tab.

  • Richard Terris 273 posts 715 karma points
    Feb 11, 2015 @ 10:13
    Richard Terris
    0

    Are you using some code-first framework?

    I suspect the answer here will be to go through the document types manually in the back office and change the tabs the properties are on.

  • MuirisOG 382 posts 1284 karma points
    Feb 11, 2015 @ 11:25
    MuirisOG
    0

    Hi Richard, Here's a bit more information.

    I'm migrating an old CMS into Umbraco and would like to create the document type in code.

    After adding Property Types to the Document Type, I tried to loop through them and assign them to a new tab.

    However, the code below isn't working.

    From what I can work out, the new properties have been added to the "Generic" tab, which doesn't seem to be accessible by calling getVirtualTabs.

    Although I can set the tab individually for each new property type (there are a lot), it would be nice to know if there is a way to just loop through them all. I've commented out the bit that works. (Note I'm using VB.NET - this is all in a button click handler)

        Dim vUser As User = New User(1)
        Dim vDT As DocumentType = DocumentType.MakeNew(vUser, "Test DocType from code")
        vDT.Description = "This is a test DocType from code"
        vDT.Alias = "TestDocType"
    
        ' new Tab for this document type to hold Old CMS values
        Dim vTabID = vDT.AddVirtualTab("Old CMS Properties")
    
        ' create first of new property types - note it generates a return value
        Dim vpType As PropertyType
        vpType = vDT.AddPropertyType(New DataTypeDefinition(-49), "YesNoProperty", "Old CMS Boolean Value")
    
        '' This works! - but has to be done one at a time for each new property
        '' set the tab for all these new properties
        'vDT.SetTabOnPropertyType(vDT.getPropertyType("YesNoProperty"), vTabID)
    
        '' This doesn't work!
        For Each vTab As DocumentType.Tab In vDT.getVirtualTabs
            For Each vPT As PropertyType In vTab.GetAllPropertyTypes
                ' set the tab for all these new properties
                vDT.SetTabOnPropertyType(vPT, vTabID)
            Next
        Next
    
        vDT.Save()
    
  • Richard Terris 273 posts 715 karma points
    Feb 11, 2015 @ 11:31
    Richard Terris
    0

    Can I ask why you don't just export the document types from the old site and import them into the new one?

  • MuirisOG 382 posts 1284 karma points
    Feb 11, 2015 @ 11:37
    MuirisOG
    0

    Unfortunately, the old site isn't an Umbraco site (it's Immediacy).

    We've got a number of them and I'm trying to automate as much as possible the import routines.

    I know I've hard coded the property type in my sample code above, but the ultimate aim is to grab the relevant fields from our old cms automatically, so whoever runs the import jobs won't have to delve into code.

  • Richard Terris 273 posts 715 karma points
    Feb 11, 2015 @ 11:46
    Richard Terris
    0

    I use uSiteBuilder in my development and while there is a camp of people against code first, it might be perfect for your needs.

    You can create document types in C# including all the properties and tabs.

    So I'm sure you could write a generic class builder to accept the property names as parameters and somehow work out what the tabs should be (you can have a generic list of tabs in an enum to choose from) and create the class files from your class builder.

    Then when you compile your solution the document types will be created in Umbraco

  • MuirisOG 382 posts 1284 karma points
    Feb 11, 2015 @ 11:52
    MuirisOG
    0

    Many thanks, Richard,

    I use VS2012, and the intellisense is pretty good, but if uSiteBuilder is built with Umbraco in mind, it's definitely worth a look.

  • Richard Terris 273 posts 715 karma points
    Feb 11, 2015 @ 11:54
    Richard Terris
    0

    uSiteBuilder is a class library designed specifically for creating Umbraco document types and data types in code

    https://github.com/spopovic/uSiteBuilder

    I'd take a look at their website too http://usitebuilder.vegaitsourcing.rs/ though their tutorials are possibly outdated since it's now on V2

  • MuirisOG 382 posts 1284 karma points
    Apr 17, 2015 @ 12:23
    MuirisOG
    0

    Update on Property Types

    As a newbie, I didn't really appreciate how much difference there is between the older and newer versions of Umbraco.

    The code above was for version 4, so I thought I better post a code snippet on how I've done something similar in version 7 using VB.NET (note that I used C style comments below as otherwise the formatting was off)

    (I know... I could've just exported the document type from version 4, but where's the fun in that!?)

    Please let me know if I'm completely off-track on this.

    I'm using

    • Umbraco 7.2.4
    • WebForms
    • a usercontrol written in VB.NET

      //'set some variables for the services used below
      Dim cts As IContentTypeService = ApplicationContext.Current.Services.ContentTypeService
      Dim fs As IFileService = ApplicationContext.Current.Services.FileService
      Dim dts As IDataTypeService = ApplicationContext.Current.Services.DataTypeService
      
      
      //'create a new doctype
      Dim doctype As New ContentType(-1)
      
      
      //'set some initial properties for the new doctype
      doctype.Alias = "importDocType"
      doctype.Name = "DocType for imported data"
      doctype.Description = "This DocType will hold fields imported from the old CMS"
      doctype.SortOrder = 1
      doctype.CreatorId = 0
      //'note: you could also assign the doctype.icon and doctype.thumbnail here
      
      
      //'Should this be AllowedAtRoot???
      doctype.AllowedAsRoot = True
      
      
      //'set the default template (based on our old CMS but being rewritten with responsive design principles in mind)
      //'note: this seems to also set the AllowedTemplates attribute
      Dim docTemplate As Template = fs.GetTemplate("TemplateForImportedCMSFields")
      doctype.SetDefaultTemplate(docTemplate)
      
      
      //'create a tab for this document type to hold imported values
      doctype.AddPropertyGroup("Imported Properties")
      
      
      //'*** the code below must be replicated for all the fields we need to import
      //'create a numeric data field in the document type
      Dim dataTypeDef As DataTypeDefinition = dts.GetDataTypeDefinitionById(-51)
      Dim propType = New umbraco.Core.Models.PropertyType(dataTypeDef)
      propType.Name = "Old CMS Page ID"
      propType.Alias = "old_page_id"
      doctype.AddPropertyType(propType, "Imported Properties")
      //'*** end the replicated code
      
      
      //'at the end, make sure to save the changes
      cts.Save(doctype)
      
Please Sign in or register to post replies

Write your reply to:

Draft