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

Can someone give me an example of how to programmatically 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?

+6
source share
2 answers

I managed to fix it. Recently, the site was upgraded from Umbraco 4.5 to Umbraco 4.7.1, so the dll had to be replaced with later ones. In the older version of Umbraco, the return type was public void AddPropertyType , while the new public PropertyType AddPropertyType . Apparently, during the upgrade, the new cms.dll was not copied, so I copied it from a clean Umbraco 4.7.1 solution, changed the code to get the return type, and that helped.

Required Namespaces:

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

Thus, the last code (assuming the assembly is correct):

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

This code looks good to me, it should work.

Make sure your first line actually returns the document type, not null.

In addition, you have the right β€œgames” in place, do you need at least some of them?

 using umbraco.cms.businesslogic.web; using umbraco.NodeFactory; using umbraco.cms.businesslogic.member; using umbraco.cms.businesslogic.datatype; 
0
source

Source: https://habr.com/ru/post/916479/


All Articles