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

故事扮演 提交于 2019-12-04 06:14:50

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

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