Delete all the records of the ContactUs form

Theodoulos Iacovou asked on March 4, 2020 13:47

I have add the bellow code in a scheduling task and does not delete the records of my form any suggestion?

using CMS.Scheduler;
using CMS.EventLog;
using CMS;
using CMS.OnlineForms;
using CMS.SiteProvider;
using CMS.DataEngine;

[assembly: RegisterCustomClass("Custom.CustomTask", typeof(Custom.CustomTask))]

public class CustomTask : ITask
{
    public string Execute(TaskInfo task)
    {
        throw new System.NotImplementedException();
    }
}

namespace Custom
{
    public class CustomTask : ITask
    {
        /// <summary>
        /// Executes the task.
        /// </summary>
        /// <param name="ti">Info object representing the scheduled task</param>
        public string Execute(TaskInfo ti)
        {
            string details = "Custom scheduled task executed. Task data: " + ti.TaskData;

            // Logs the execution of the task in the event log
            EventLogProvider.LogInformation("CustomTask", "Execute", details);
            // Gets the form object representing the 'ContactUs' form on the current site
            BizFormInfo formObject = BizFormInfoProvider.GetBizFormInfo("ContactUs", SiteContext.CurrentSiteID);

            if (formObject != null)
            {
                // Gets the class name of the 'ContactUs' form
                DataClassInfo formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
                string formClassName = formClass.ClassName;

                // Loads all data records from the form that have an empty value in the 'UserMessage' field
                ObjectQuery<BizFormItem> data = BizFormItemProvider.GetItems(formClassName)
                                                                        .WhereEmpty("FullAddress");

                // Loops through the form's data records
                foreach (BizFormItem item in data)
                {
                    // Deletes all files stored in the form's fields
                    BizFormInfoProvider.DeleteBizFormRecordFiles(formClass.ClassFormDefinition, item, SiteContext.CurrentSiteName);

                    // Deletes the form record from the database
                    item.Delete();
                }
            }
            // Returns a null value to indicate that the task executed successfully
            // Return an error message string with details in cases where the execution fails
            return null;
        }
    }
}

Recent Answers


Roman Hutnyk answered on March 4, 2020 15:48

Have you registered this task in Kentico admin and have it executed?

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 4, 2020 15:50 (last edited on March 4, 2020 15:58)

Yes I have added this code in a class..Is the structure of the code wrong??

0 votesVote for this answer Mark as a Correct answer

Arjan van Hugten answered on March 4, 2020 16:11 (last edited on March 4, 2020 16:13)

You need to create a scheduled task in the CMS 'Scheduled tasks' application. Just read and follow the whole documentation page please. https://docs.kentico.com/k12/configuring-kentico/scheduling-tasks/scheduling-custom-tasks

There you can configure your custom task to be used as a scheduled task.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 4, 2020 16:18

Code looks fine, the issue is that system does not executes it currently. As Arjan said, you need to register scheduled task in Kentico admin and either run/execute a task manually or schedule execution.

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 4, 2020 17:05

The task is in a scheduled task and it does not work

0 votesVote for this answer Mark as a Correct answer

Eric Dugre answered on March 4, 2020 19:38

What do you mean when you say "does not work?" Does the task execute? Is there any error message in Last result?

Have you tried debugging the task? Maybe the code name of the form isn't "ContactUs" but rather something like "ContactUs_1" so nothing is happening.

Does SiteContext.CurrentSiteID resolve to the correct value?

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 4, 2020 20:01

Your code adds event log record - can you see it?

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 5, 2020 10:08 (last edited on March 5, 2020 10:59)

The task has executed but the records are still in the database.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on March 5, 2020 11:17

Have you tried to debug it?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on March 5, 2020 12:28

Hi Theodoulos,

Looks like you don't need this code:

[assembly: RegisterCustomClass("Custom.CustomTask", typeof(Custom.CustomTask))]

public class CustomTask : ITask
{
    public string Execute(TaskInfo task)
    {
        throw new System.NotImplementedException();
    }
}

It might be confusing and you might have misselected wrong implementation in Scheduled Tasks application. Could you please post a screenshot of your task configuration in Kentico?

1 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 5, 2020 13:45

I delete this code i get error message.Can someone check this code?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on March 5, 2020 13:56

What eeror message do you get? And where?

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 5, 2020 13:59

I get error on scheduling task.He cannot define the table where needs to select.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on March 5, 2020 14:02

Could you post a screenshot or text of the error? Your explanation doesn't give much details. It's really hard to guess what you are talking about...

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 5, 2020 14:09

Error: Custom class 'Custom.CustomTask' was not found. Please make sure you register it properly using RegisterCustomClass attribute, or that you provide it through OnGetCustomClass event.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on March 5, 2020 14:35

You need to keep this line in the code: [assembly: RegisterCustomClass("Custom.CustomTask", typeof(Custom.CustomTask))]
Delete just the duplicate method definition:

**public class CustomTask : ITask { public string Execute(TaskInfo task) { throw new System.NotImplementedException(); }}**

0 votesVote for this answer Mark as a Correct answer

Theodoulos Iacovou answered on March 5, 2020 14:52

1)I have create a class on Visual Studio where I added this code 2)Next , on Scheduling task I have added this task 3)Next, when i am running this task the data of the forms continue to exist in the database

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on March 5, 2020 15:56

Ok, few more questions for Theodoulos:

  1. Are you using Kentico 12 MVC? What is the exact version?
  2. Is MVC, have you written your task code only for CMS (non-MVC) solution?
0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on March 5, 2020 16:29

I've just tried the following code on fresh installation of Kentico 12 MVC and it worked well. I've added this class to CMSApp CMS project, into CMSModules folder:

using CMS.DataEngine;
using CMS.EventLog;
using CMS.OnlineForms;
using CMS.Scheduler;
using CMS.SiteProvider;

namespace CMSApp.CMSModules
{
    public class CustomTask : ITask
    {
        public string Execute(TaskInfo ti)
        {
            var details = "Custom scheduled task executed. Task data: " + ti.TaskData;

            EventLogProvider.LogInformation("CustomTask", "Execute", details);
            var formObject = BizFormInfoProvider.GetBizFormInfo("DancingGoatMvcContactUsNew", SiteContext.CurrentSiteID);

            if (formObject == null) return null;

            var formClass = DataClassInfoProvider.GetDataClassInfo(formObject.FormClassID);
            var formClassName = formClass.ClassName;

            var data = BizFormItemProvider.GetItems(formClassName)
                .WhereEmpty("TestText");

            foreach (var item in data)
            {
                BizFormInfoProvider.DeleteBizFormRecordFiles(formClass.ClassFormDefinition, item, SiteContext.CurrentSiteName);
                item.Delete();
            }

            return null;
        }
    }
}

This is how the task was configured in Kentico (registering custom class is not mandatory, you can just select ITask implementation from the assembly you are using):

task config

In the code I was using DancingGoatMvcContactUsNew as form code name:

code name

And this is the data before running the task:

data before

This is after (you can see that all the records with empty Test text were removed):

data after

The only other reason I can think of is that you may have some custom module that uses some Form events and cancels the delete operation or something, but that would have been strange..

1 votesVote for this answer Mark as a Correct answer

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