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! ;-)
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?
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
Hi Chris,
It's a bit rough-n-ready, here's a quick code sample:
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.
Cheers Lee, I'll let you know how I get on.
Chris
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
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.
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?
is working on a reply...