Unique Identifier for Widget on a page

Chris Bass asked on July 19, 2018 22:49

(I think this is a version-independent question, but I am currently working with this issue on a Kentico 8.0 site.)

I would like to set up a unique identifier per instance of a certain widget on a page - I'll be using this for an anchor tag, but the anchor is within the same widget (Bootstrap collapse) so it can be a property of the widget. I don't need it to be consistent between page loads, just unique per widget instance on the page.

I tried setting a property of the widget as 'identifier' to Math.GetRandomInt(), but it happens so fast that multiple of them end up using the same random number, despite it being recalculated per item.

I also tried setting a random identifier to the widget on creation of it, but if you copy/paste the widget the copy keeps the same shared id.

The content of some of the collapsible regions may be the same, so just hashing a user-entered field on the content isn't sufficient.

And I'd rather not ask the user to manually type in a random id to each one.

A few years ago there was a similar question to this: https://devnet.kentico.com/questions/add-id-in-widget-properties

But they only needed one per page so were able to autogenerate one based on the page name.

Correct Answer

Peter Mogilnitski answered on July 20, 2018 01:54

Since web parts are inhered from abstract web part class you can use: InstanceGUID property.
Wigets are based on WebPartInstance class, they have as well their InstanceGUID .

P.S. here are some widget API classes you might need

0 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on July 20, 2018 01:23

If you're using C# why not use Guid.NewGuid() Prob a lot more random than GetRandomInt()

0 votesVote for this answer Mark as a Correct answer

Chris Bass answered on July 20, 2018 16:21 (last edited on July 20, 2018 16:22)

To clarify, if I am doing this widget based on a custom web part, yes, I can easily call Guid.NewGuid() and generate a unique ID. This is based on the basic Static HTML widget from Kentico, and I'm looking for a solution directly in the widget, without customizing the web part.

@Brenden Sorry, due to the above (which I admit I failed to be clear on in my original question) I needed the solution to be in the widget itself or its (Text/K#) container.

@Peter Referencing {% InstanceGUID %} actually worked perfectly, thank you!

1 votesVote for this answer Mark as a Correct answer

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