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?
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.
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.
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
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
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
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
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
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
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
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.
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
Sorry, the second link got a bit chewed up.
It's...
https://our.umbraco.org/forum/developers/api-questions/43278-programmatically-creating-a-document-type
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:
I hope this can help others with similar requirements.
Best, Sören
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.
Hi MuirisOG,
thanks for sharing your code!
Best, Sören
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.
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
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!
Thanks :)
I've just discovered SaveDataTypeAndPreValues()... hopefully I'm now on the right track.
is working on a reply...