Auto Increment Field in Page Type

Aasim Khan asked on September 1, 2015 10:30

Hello Team,

How can we create auto increment field in page type.

Please suggestion me.

Regards, Aasim Afridi

Recent Answers


Dawid Jachnik answered on September 1, 2015 12:39 (last edited on September 1, 2015 15:56)

Hello,

What do you want to increment? value of hte field after each update ?

if yes, then you have here the custom handler class which will be increment your field:

using System;
using CMS.Base;
using CMS.DocumentEngine;

[DocumentCustomHandler]
public partial class CMSModuleLoader
{

private class DocumentCustomHandler : CMSLoaderAttribute
{
    public override void Init()
    {
        DocumentEvents.Update.Before += Document_Update_Before;
    }
    private void Document_Update_Before(object sender, DocumentEventArgs e)
    {
        TreeNode doc = e.Node;
        if (doc.ClassName.ToLower() == "yourdocumentclassname")
        {
            int oldValue = doc.GetValue<int>("IncrementationField", 0);
            e.Node.SetValue("IncrementationField", oldValue++);
        }
    }
}

}

2 votesVote for this answer Mark as a Correct answer

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