+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Friendly Unique ID

Friendly Unique ID

4 posts, 0 answered
  1. Shawn
    Shawn avatar
    79 posts
    Registered:
    26 Sep 2012
    31 Aug 2010
    Link to this post
    Sitefinity comes out of the box using GUIDs as identifiers.  I have a requirement where I must display a "friendly" unique id at the bottom of content so it can be easily referenced.  My initial thought was to add an int identity field to the sf_CmsContentBase table.  This would make it easy to generate the unique id.  However, it should really be a meta data field since I would like to easily access it via databinding, insert/updates, etc.  Does anyone have a suggestion or best practice on how to implement a friendly unique id in Sitefinity?

    My second thought was to generate a random, readable id and query the API for an existing content item.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    01 Sep 2010
    Link to this post
    Hello Shawn,

    If you work with content items (IContent ) and dynamic fields ( metakeys) you do not have to make any changes to the database. You can create  a class that generates random "int" each time you create a new content items. This value will be automatically persisted ( you can take a look at UserManual , page 214 - Custom Fields)
    To pass the random number to this filed you can create a custom class that inherits from ContentItemNew and override SaveContent() method.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Shawn
    Shawn avatar
    79 posts
    Registered:
    26 Sep 2012
    01 Sep 2010
    Link to this post
    I understand when working with metafields you do not have to make database changes.  I'm very well versed in building custom modules, etc.  The key word is "unique" int field.  I could easily generate a random int but I have to guarantee it's unique.  Adding an int identity field to the table is an easy way to guarantee a unique int...however I don't really want to modify the data model. I'd much rather find a better solution that leverages the meta fields. 

    My next option was to generate a random number and then query the api for an content items that have the same id.  However, I might end up doing a bunch of queries if it happens to select a random number that's already in use. Any input is appreciated.   
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    01 Sep 2010
    Link to this post
    Hello Shawn,

    If the int is about 6-7 digits the possibility to get the same number twice is very small. You can use a table with surrogate keys or Dictionary of two or more parameters. Then you can use C# indexer or LINQ -SQL or GetContent(id) method. In this case you should have two parameters - content ID and your int.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
4 posts, 0 answered