Access MVC Widget Identifier

Budd Wright asked on March 7, 2019 02:17

In MVC widgets, in the widget's controller, how can we access a widget's unique identifier (GUID)?

It's there in the CMS_Document DocumentPageBuilderWidgets column:

{ "editableAreas": [ { "identifier": "Sections", "sections": [ { "identifier": "98b9832d-c53d-42a7-b1a2-4b6e6b91de84", "type": "Kentico.DefaultSection", "zones": [ { "identifier": "090c6b62-77da-4c30-a4e4-935bcd97ec5a", "widgets": [ { "identifier": "31481c30-04ed-40de-b500-1e4618cd17c6", <-- THIS

This property would be very valuable in many scenarios. I thought to add a GUID property to the widget's property class, and maybe use the HiddenGuidInputComponent component as an editing component, thinking maybe there would be a way to use that to create a new GUID at runtime, but that doesn't appear to be possible.

Alternatives?

Recent Answers


Budd Wright answered on March 7, 2019 02:25

For example, if I want to cache some expensive process within a widget controller, it would help to have a unique ID for the cache key. (Preferred over CacheOutput so that widgets that appear multiple times on a page with different content can be individually cached, for example.)

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 7, 2019 17:23

I think you are talking about InstanceGUID property. Here the topic on it.

0 votesVote for this answer Mark as a Correct answer

Budd Wright answered on March 7, 2019 19:09

Peter, this is for the new MVC widgets in Kentico 12. Not widgets in the Portal engine or Kentico side.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 7, 2019 20:44

well, I guess you can define your own property for you widget? Something like below?

public class MyWidgetProperties : IWidgetProperties
{
    public string InstanceGUID { get; set; } = Guid.NewGuid();
}
0 votesVote for this answer Mark as a Correct answer

Budd Wright answered on March 7, 2019 21:11

Peter, I was messing around with that idea last night, but something didn't check out, or I thought that on edit, Kentico wouldn't reset the value since it already would have a default GUID in place ... thinking about it now, that may not be the case, so I'll give that a try again and see if that checks out. Totally possible I overthought it! ;)

0 votesVote for this answer Mark as a Correct answer

Budd Wright answered on March 7, 2019 21:17

Sure enough, I overthought it! Kentico does indeed honor that GUID when setting a default with lambda expression. Thanks, Peter!

0 votesVote for this answer Mark as a Correct answer

Budd Wright answered on March 8, 2019 03:56

Actually... this solution only helps you if your widget HAS these extra properties classes. Not all do. For widgets without properties, you're still out an identifier. I've submitted a support ticket to Kentico, because the widget's identifier should definitely be exposed, and could easily be done looking at the underlying code base.

0 votesVote for this answer Mark as a Correct answer

Steve Harris answered on March 10, 2019 18:01

MVC widgets in Kentico represent reusable components that can easily be manipulated by content editors and other non-technical users. Widgets and the Kentico page builder feature give said non-technical users more power and flexibility when adjusting page content, in addition to basic editing of text and images.

0 votesVote for this answer Mark as a Correct answer

Budd Wright answered on March 11, 2019 00:19

Kentico support responded to this issue saying they agree an identifier needs to be exposed and will be added in a future hotfix or the service pack.

3 votesVote for this answer Mark as a Correct answer

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