ClassDisplayName

Charles Matvchuk asked on March 5, 2017 18:01

Apparently I am challenged today. I need to get the "ClassDisplayName" in c# in my code behind for the current page (Page Type) a custom webpart is on. I have been trying to work with PageInfo and DocumentContext without success.

It is probably very easy, not sure why I am missing it.

Thanks.

Correct Answer

Roman Hutnyk answered on March 6, 2017 07:35

Charles, I'm not sure you can get it from a current document object, however you could retrieve it from a database:

var doc = DocumentHelper.GetDocument(DocumentContext.CurrentDocument.DocumentID, new TreeProvider());

CMS.DataEngine.DataClassInfoProvider.GetDataClassInfo(doc.ClassName);
2 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on March 5, 2017 23:31

It happens to all of us!

The class can be found through the CMS_Tree table, so if you have the current document's NodeID, you can use the CMS.DocumentEngine's either TreeHelper or DocumentHelper (TreeHelper i think has a GetSingleNode where you can pass the NodeID).

Once you have the TreeNode object, you may be able to pull the ClassName from it, if not then grab the ClassID and use the ClassInfoProvider to get the class from the ID, and then the class name from that object.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on March 6, 2017 00:14 (last edited on March 6, 2017 00:14)

Trevor,

I have been down that path to no avail. Also TreeHelper is deprecated in V10. Do you have a code example to get the current document class display name ?

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on March 6, 2017 14:37 (last edited on March 6, 2017 14:39)

Roman, That does not work. The return for that code is incorrect. Any other ideas?

I am using the following now, but was wondering whether there was something a bit more clean. Unfortunately this requires me to settle on part of the class name rather than what is in the ClassDisplayName.

DocumentContext.CurrentDocument.ClassName.Split('.')[1];

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 6, 2017 14:40

What exactly is incorrect? CMS.DataEngine.DataClassInfoProvider.GetDataClassInfo(doc.ClassName) returns DataClassInfo object which has many properties including class display name.

0 votesVote for this answer Mark as a Correct answer

Charles Matvchuk answered on March 6, 2017 14:43

Roman, you are correct. Apparently I am unable to write code this morning, maybe after another cup of coffee.

Thanks so much for the answer.

0 votesVote for this answer Mark as a Correct answer

Cland Martin answered on July 19, 2018 12:56

Instead of ToString you need to access the DisplayName property. You can do that by casting to DisplayNameAttribute. liteblue

var classDisplayName =
((DisplayNameAttribute)
typeof(Opportunity).GetCustomAttributes(typeof(DisplayNameAttribute), true).FirstOrDefault()).DisplayName;
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.