Auto Increment / Identity Custom Field Type

[亡魂溺海] 提交于 2019-12-06 15:59:43

You can use the following code:

static string uniqueCode()
{
    string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#";
    string ticks = DateTime.UtcNow.Ticks.ToString();
    var code = "";
    for (var i = 0; i < characters.Length; i += 2)
    {
        if ((i + 2) <= ticks.Length)
        {
            var number = int.Parse(ticks.Substring(i, 2));
            if (number > characters.Length - 1)
            {
                var one = double.Parse(number.ToString().Substring(0, 1));
                var two = double.Parse(number.ToString().Substring(1, 1));
                code += characters[Convert.ToInt32(one)];
                code += characters[Convert.ToInt32(two)];
            }
            else
                code += characters[number];
        }
    }
    return code;
}

This code will generate a unique code / key based on universal time stamp ticks.

More details in this link

nsgocev

You can achieve this via custom token instead of a custom field. I already answered a similar question here: Sitecore - Custom field, add unique value on create

Basically you will create a custom token and add your generation algorithm inside. You can use Guid.NewGuid() apply some kind of hash over it and pick as many characters as you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!