Run C# code after update custom class in custom module

GTEK IT DEV Team asked on August 16, 2019 12:14

I have a class in a custom module. Now i want to run a C# funtion after update data event of that class. What i need to do for that situation?

Thanks.

Recent Answers


Dragoljub Ilic answered on August 16, 2019 12:32

Hi,

If I understand you well, you should register object event in your custom module initialization class: ForumGroupInfo.TYPEINFO.Events.Insert.After += ForumGroup_InsertAfterEventHandler;

and then handle it like this:

private void ForumGroup_InsertAfterEventHandler(object sender, ObjectEventArgs e)
{
    // Checks that the forum group was successfully created
    if (e.Object != null)
    {
        // Gets an info object representing the new forum group
        ForumGroupInfo forumGroup = (ForumGroupInfo)e.Object;

        // Creates the child forum object
        ForumInfo newForum = new ForumInfo()
        {
            // Sets the child forum's properties
            ForumDisplayName = forumGroup.GroupDisplayName + " - General",
            ForumName = forumGroup.GroupName + "_General",
            ForumGroupID = forumGroup.GroupID,
            ForumSiteID = forumGroup.GroupSiteID,

            ForumOpen = true,
            ForumModerated = false,
            AllowAccess = SecurityAccessEnum.AllUsers,
            ForumThreads = 0,
            ForumPosts = 0
        };

        // Saves the child forum to the database
        ForumInfoProvider.SetForumInfo(newForum);
    }
}

You can find full example on this link in documentation.

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

GTEK IT DEV Team answered on August 16, 2019 14:20

Thanks Dragoljub Ilic, But i cannot do that with CMS.DocumentEngine.TreeNode with following code snippet

[assembly: RegisterModule(typeof(DocumentEvents))]
public class DocumentEvents : Module
{
    public DocumentEvents()
        : base("CMS.DocumentEngine")
    {
    }

    // Contains initialization code that is executed when the application starts
    protected override void OnInit()
    {
        base.OnInit();
    }
}

I received error live this: An item with the same key has already been added.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on August 16, 2019 15:58

Hi,

You can use this snippet for document events:

DocumentEvents.Update.After += Document_Update_After;

Please refer to Handling Global Events and this articles,

0 votesVote for this answer Mark as a Correct answer

GTEK IT DEV Team answered on August 16, 2019 16:35

I used fowllowing code:

protected override void OnInit()
{
    base.OnInit();
    DocumentEvents.Update.Before += Update_Before;
}

private void Update_Before(object sender, DocumentEventArgs e)
{
    TreeNode currentDocument = e.Node;
}

But after each time i press save button. The function Update_Before was called 4 times. Why this happened?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on August 16, 2019 19:19

Have you checked e.Node object if it is the same in 4 hits? It may be there are different objects triggering this event.

0 votesVote for this answer Mark as a Correct answer

GTEK IT DEV Team answered on August 19, 2019 04:37

I don't see the differents between objects. They have the same DocumentID and DocumentNodeID.

0 votesVote for this answer Mark as a Correct answer

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