Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a property to a document type in Umbraco from code?

Could anyone give me an example of how to programatically add a property to an existing document type in Umbraco CMS? This is what I tried:

var dt = DocumentType.GetByAlias("TestDocType");
dt.AddPropertyType(new DataTypeDefinition(-49),"testprop", "test prop");

But it throws an exception:

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

Any ideas?

like image 406
Donatas Bacius Avatar asked May 23 '12 16:05

Donatas Bacius


1 Answers

I managed to fix it. The website was recently upgraded from Umbraco 4.5 to Umbraco 4.7.1, so the dll's had to be replaced with the more recent ones. In the older version of Umbraco the method's return type was public void AddPropertyType whereas the new one public PropertyType AddPropertyType. Apparently during the upgrade the new cms.dll wasn't copied over, so I copied it from a clean Umbraco 4.7.1 solution, changed the code to receive the return type and it helped.

Required namespaces:

using umbraco.cms.businesslogic.datatype;
using umbraco.cms.businesslogic.web;

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

var dt = DocumentType.GetByAlias("TestDocType");
var pType = dt.AddPropertyType(new DataTypeDefinition(-49),"testprop", "test prop");
like image 107
Donatas Bacius Avatar answered Oct 04 '22 18:10

Donatas Bacius