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

对着背影说爱祢 提交于 2019-12-21 14:46:42

问题


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?


回答1:


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");



回答2:


That code looks fine to me, it should work.

Make sure your first line is actually returning a documentype, not null.

Also, do you have the proper 'usings' in place, you'll need at least some of these?

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


来源:https://stackoverflow.com/questions/10724473/how-to-add-a-property-to-a-document-type-in-umbraco-from-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!