Copied to clipboard

Flag this post as spam?

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


  • Sören Deger 733 posts 2844 karma points c-trib
    Jun 24, 2015 @ 03:55
    Sören Deger
    0

    How create new document type with service?

    Hi, how I can create new document types and properties with services? I had a look at ContentTypeService, but it seems that this is not supported.

    Any ideas?

    Best, Sören

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 24, 2015 @ 05:03
    Jan Skovgaard
    2

    Hi Sören

    I don't think that it's possible - To me it sounds a bit like you're after a "Code first" approach?

    If so then you should perhaps check out uSitebuilder? https://github.com/spopovic/uSiteBuilder - But before going down that route I think it's worth reading this blogpost https://offroadcode.com/blog/1774/the-state-of-code-first-development-in-umbraco/ and see this video with Pete Duncanson where he explains why it's not a good idea https://www.youtube.com/watch?v=Hr1irQ0h5J8

    Hope this helps.

    /Jan

  • Sören Deger 733 posts 2844 karma points c-trib
    Jun 24, 2015 @ 05:31
    Sören Deger
    0

    Hi Jan,

    thanks. I will create a nuget package for my mail2cms package. But I have 4 document types in this package and don't know how I can put these to the nuget package. Any other ideas?

    Cheers, Sören

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 24, 2015 @ 06:18
    David Brendel
    1

    Hi Sören,

    would recommend a workaround. Just hook into Applications Start. Check some flag. Install document types. Set flag.

    Or maybe it is possible to run c# code from a powershell script. Cause nuget calls one if it is present after/during package install.

    Think these are the possibilities you have.

    Regards David

  • Sören Deger 733 posts 2844 karma points c-trib
    Jun 24, 2015 @ 06:24
    Sören Deger
    0

    Hi David,

    thanks! Yes, I will hook into Application Start. But I'm missing a method of an existing Service for this. Maybe I must use the old code similar like the CreateMedia method in DAMP-Package (http://damp.codeplex.com/SourceControl/latest#DAMP.Samples/Installer.ascx.cs), but for creating content types instead of media types.

    Best, Sören

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Jun 24, 2015 @ 06:39
    David Brendel
    1

    Hi Sören,

    think you could use the ContentTypeService for this. So create a new ContentType and save it with the service. https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Core/Services/ContentTypeService.cs

    That is also how you should handle creating media types.

    Or does I missunderstand something?

    Regards David

  • Sören Deger 733 posts 2844 karma points c-trib
    Jun 24, 2015 @ 06:51
    Sören Deger
    0

    Hi David,

    thanks. I think this can works :-) I will give it a try this evening and give you feedback tomorrow! If it will works, I share the code here for all.

  • MuirisOG 382 posts 1284 karma points
    Jun 24, 2015 @ 13:56
    MuirisOG
    0

    Hi Sören, I've got some VB.net code you could look at, but do consider uSiteBuilder as well. Let me know if you'd like me to post it here.

    Also, have a look at these pages

    http://refreshwebsites.co.uk/blog/umbraco-document-types-explained-in-60-seconds/ https://our.umbraco.org/forum/developers/api-questions/43278-programmatically-creating-a-document-type

  • MuirisOG 382 posts 1284 karma points
    Jun 24, 2015 @ 13:58
  • Sören Deger 733 posts 2844 karma points c-trib
    Jun 26, 2015 @ 02:37
    Sören Deger
    103

    Hi all,

    thank you for showing the possibilities and links to the basic code samples :-) I have solved it with the ApplicationStarted Event. I had to search a little bit longer, but I have found small parts of a solution for my requirement in several other posts. After checking a flag I have finally this code inside:

                var cs = ApplicationContext.Current.Services.ContentTypeService;
                Umbraco.Core.Models.ContentType ctEmail = new Umbraco.Core.Models.ContentType(-1);
                ctEmail.Name = "mail2cms_email";
                ctEmail.Alias = "mail2cms_email";
                 ctEmail.Icon = "icon-message";
                ctEmail.AddPropertyGroup("Content");
                ctEmail.AddPropertyGroup("Header");
    
                var textstringDef = new DataTypeDefinition(-1, "Umbraco.Textbox");
                var richtextEditorDef = new DataTypeDefinition(-1, "Umbraco.TinyMCEv3");
                var textboxMultipleDef = new DataTypeDefinition(-1, "Umbraco.TextboxMultiple");
    
                ctEmail.AddPropertyType(new PropertyType(textstringDef) { Alias = "subject", Name = "Subject", Description = "", Mandatory = false, SortOrder = 1}, "Content");
                ctEmail.AddPropertyType(new PropertyType(richtextEditorDef) { Alias = "bodyText", Name = "Message", Description = "", Mandatory = false, SortOrder = 2 }, "Content");
                ctEmail.AddPropertyType(new PropertyType(textboxMultipleDef) { Alias = "attachements", Name = "List of attachments", Description = "", Mandatory = false, SortOrder = 3 }, "Content");
    
                ctEmail.AddPropertyType(new PropertyType(textstringDef) { Alias = "from", Name = "From", Description = "", Mandatory = false, SortOrder = 4 }, "Header");
                ctEmail.AddPropertyType(new PropertyType(textstringDef) { Alias = "to", Name = "To", Description = "", Mandatory = false, SortOrder = 5 }, "Header");
                ctEmail.AddPropertyType(new PropertyType(textstringDef) { Alias = "cc", Name = "CC", Description = "", Mandatory = false, SortOrder = 6 }, "Header");
                ctEmail.AddPropertyType(new PropertyType(textstringDef) { Alias = "bcc", Name = "BCC", Description = "", Mandatory = false, SortOrder = 7 }, "Header");
                ctEmail.AddPropertyType(new PropertyType(textstringDef) { Alias = "importDate", Name = "Import date", Description = "", Mandatory = false, SortOrder = 8 }, "Header");
    
                cs.Save(ctEmail);
    

    I hope this can help others with similar requirements.

    Best, Sören

  • MuirisOG 382 posts 1284 karma points
    Jun 26, 2015 @ 12:41
    MuirisOG
    1

    Hi Sören yours is a bit more elegant than what I did in VB.net. But for completeness, I've shown some sample code below. Note that I've used C#-style comments.

    //'To quickly test this, put it in a user control with a button on the page which, when clicked,
    //'fires off this code:
    Protected Sub createDocType(sender As Object, e As System.EventArgs) Handles btnCreateDocType.Click
        //'NB Have a look at this for better construction of document types
        //'http://refreshwebsites.co.uk/blog/umbraco-document-types-explained-in-60-seconds/
    
        Dim userService = ApplicationContext.Current.Services.UserService
    
        //'https://our.umbraco.org/forum/developers/api-questions/43278-programmatically-creating-a-document-type
        Dim cts As IContentTypeService = ApplicationContext.Current.Services.ContentTypeService
        Dim fs As IFileService = ApplicationContext.Current.Services.FileService
        Dim dts As IDataTypeService = ApplicationContext.Current.Services.DataTypeService
    
        Dim doctype As New ContentType(-1)
    
        //'set some initial properties
        doctype.Alias = "myDocType"
        doctype.Name = "My DocType"
        doctype.Description = "This DocType will hold fields of my own design"
        doctype.Icon = "icon-application-window"
        doctype.AllowedAsRoot = True
        doctype.SortOrder = 1
        doctype.CreatorId = 0
    
        //'set the default template
        //'note: this seems to also set the AllowedTemplates attribute
        //'note: you must have myHomeTemplate_en_gb already set up
        Dim docTemplate As Template = fs.GetTemplate("myHomeTemplate_en_gb")
        doctype.SetDefaultTemplate(docTemplate)
    
        //'create a tab for this document type to hold Your own values
        doctype.AddPropertyGroup("My Properties")
    
        //'create a numeric data (or whatever) field in the document type
        //' Date Picker with time (Date)
        Dim dteDataTypeDef As DataTypeDefinition = dts.GetDataTypeDefinitionById(-36)
        //'Numeric (Integer)
        Dim numDataTypeDef As DataTypeDefinition = dts.GetDataTypeDefinitionById(-51)
        //'Richtext Editor (Ntext)
        Dim rteDataTypeDef As DataTypeDefinition = dts.GetDataTypeDefinitionById(-87)
        //'Textstring (Nvarchar)
        Dim txtDataTypeDef As DataTypeDefinition = dts.GetDataTypeDefinitionById(-88)
        //'Textbox multiple
        Dim txmDataTypeDef As DataTypeDefinition = dts.GetDataTypeDefinitionById(-89)
        //'True/False (Integer)
        Dim booDataTypeDef As DataTypeDefinition = dts.GetDataTypeDefinitionById(-49)
    
        Dim numPropType01 = New umbraco.Core.Models.PropertyType(numDataTypeDef)
        numPropType01.Name = "My Property ID"
        numPropType01.Alias = "my_property_id"
        doctype.AddPropertyType(numPropType01, "My Properties")
    
        Dim txtPropType02 = New umbraco.Core.Models.PropertyType(txtDataTypeDef)
        txtPropType02.Name = "My Title"
        txtPropType02.Alias = "my_title"
        doctype.AddPropertyType(txtPropType02, "My Properties")
    
        //'carry on adding more until you have all your properties set up
    
        cts.Save(doctype)
    
    End Sub
    
  • Sören Deger 733 posts 2844 karma points c-trib
    Jun 26, 2015 @ 12:43
    Sören Deger
    0

    Hi MuirisOG,

    thanks for sharing your code!

    Best, Sören

  • MuirisOG 382 posts 1284 karma points
    Aug 06, 2015 @ 08:34
    MuirisOG
    0

    Hi Sören,

    I've hit a bit of a stumbling block when it comes to creating a checkbox list.

    I would like to create a new data type first, and then use this in my document type.

    Any help would be greatly appreciated.

  • Sören Deger 733 posts 2844 karma points c-trib
    Aug 06, 2015 @ 08:43
    Sören Deger
    0

    Hi MuirisOG,

    sorry, I have no example for this and I'm very busy the rest of this week. The next two weeks I'm on vacation.

    Maybe everyone others has an example for creating a checkbox list with ContentTypeService and can help you?

    Cheers, Sören

  • MuirisOG 382 posts 1284 karma points
    Aug 06, 2015 @ 09:27
    MuirisOG
    0

    Many thanks,

    I'll do some investigation and post back here when and if I find anything.

    As before, it will be in VB.

    PS: Enjoy your holiday!

  • Sören Deger 733 posts 2844 karma points c-trib
    Aug 06, 2015 @ 09:28
    Sören Deger
    0

    Thanks :)

  • MuirisOG 382 posts 1284 karma points
    Aug 06, 2015 @ 14:01
    MuirisOG
    0

    I've just discovered SaveDataTypeAndPreValues()... hopefully I'm now on the right track.

Please Sign in or register to post replies

Write your reply to:

Draft