Getting identifier of invoked view component

2025/01/01 7:45 PM

Is there a way to get the identifier property of a view component when you're in the view? Right now I'm simply passing the identifier as a property of the model, but I find that somewhat sloppy and was wondering if there was a better way. The model I'm using is ComponentViewModel<WidgetModelProperties> and I noticed that there is a Definition object which has what I'm looking for, but it seems inaccessible. What is the proper way to retrieve it?


Environment

  • Xperience by Kentico version: 13.0.138
  • .NET version: 8

Answers

2025/01/02 11:06 AM

As far as I can remember...

Widgets get an own internal identifier to get them referenced from the database to the location where they are used on the page. This identifier is not intended to be used externally and therefor it is not accessible out of the box. You can see more details if you look into the SQL table where the content of your page is stored including all widgets dropped on that page.

If you need an own identifier I would use an own ID property in the widget properties. It could also be a GUID, auto-set when creating an instance, and excluded from the properties editing dialog to avoid confusion for content editors.
Doing this, you can use it as identifier for your purposes without touching the internal identifier for the rendering logic from Kentico.

You should be able to access it from the Model.Properties.<YOUR-ID-FIELD>.

If you have ViewComponents without Widget context, I use to take properties from the page or template properties and map them to the view component view model in the page or template view, where the component is invoked and initialized.

2025/01/02 7:46 PM

Can you elaborate on what you're looking to accomplish with having the Identifier? Perhaps if you give us the problem you're trying to solve, we can point you in the right direction.

Are you trying to have 1 view component and alter the behavior based on what widget (of many that use it) is calling it?

To answer this question, you have to login first.