Is There A Way to Check In Every Checked Out Template/Transformation with the Kentico API?

kentico guy asked on June 8, 2021 19:41

Is there any way to check in every item that's checked out using the Kentico API? I'm using kentico 12 portal engine. I want to create a scheduled task that automatically checks in every checked out item once a week on the weekends. I am planning on using C# to do this.

EDIT: I can also use SQL if that's necessary, as I'm familiar with the tables and queries

Correct Answer

kentico guy answered on August 3, 2021 01:48

This seems to work:

[assembly: RegisterCustomClass("MyNameSpace.CheckInAllObjects", typeof(CheckInAllObjects))]
namespace MyNameSpace
{
public class CheckInAllObjects : ITask
{
    public string EventLogCode = "Check All Objects In";

    private string _scheduledTaskSuccess = "Successfully checked all applicable objects in";
    /// <summary>
    /// Main execution of task
    /// </summary>
    /// <param name="oTask"></param>
    /// <returns></returns>
    public string Execute(TaskInfo oTask)
    {
        var result = "";
        try
        {
            var transformations = TransformationInfoProvider.GetTransformations();

            foreach (var transformation in transformations)
            {
                if (transformation.ObjectSettings.ObjectCheckedOutByUserID != 0)
                {
                    TransformationInfo tempTransformation;
                    tempTransformation = transformation;
                    tempTransformation.ObjectSettings.ObjectCheckedOutByUserID = 0;
                    tempTransformation.ObjectSettings.ObjectCheckedOutVersionHistoryID = 0;
                    tempTransformation.ObjectSettings.SetValue("IsCheckedOut", false);
                    tempTransformation.ObjectSettings.SetValue("IsCheckedOutByUserID", 0);
                    TransformationInfoProvider.SetTransformation(tempTransformation);
                }
            }

            var templates = PageTemplateInfoProvider.GetTemplates();

            foreach (var template in templates)
            {
                if (template.ObjectSettings.ObjectCheckedOutByUserID != 0)
                {
                    PageTemplateInfo temptemp;
                    temptemp = template;
                    temptemp.ObjectSettings.ObjectCheckedOutByUserID = 0;
                    temptemp.ObjectSettings.ObjectCheckedOutVersionHistoryID = 0;
                    temptemp.ObjectSettings.SetValue("IsCheckedOut", false);
                    temptemp.ObjectSettings.SetValue("IsCheckedOutByUserID", 0);
                    PageTemplateInfoProvider.SetPageTemplateInfo(temptemp);
                }
            }
            result = _scheduledTaskSuccess;
        }
        catch (Exception ex)
        {
            result = ex.Message;
        }
        // then restart the application so the objects show checked in status in admin cp
        CMS.Helpers.SystemHelper.RestartApplication(); 
        return result;
    }
}
}

Add that into a cs code file then create a new scheduled task and select it as an assembly

0 votesVote for this answer Unmark Correct answer

Recent Answers


Juraj Ondrus answered on June 14, 2021 11:32

You need to get the transformation info using e.g. GetTransformationInfo method. Then, you need to "update" the transformation object properties like TransformationCheckedOutByUserID (set it to 0), TransformationCheckedOutFilename (""), TransformationCheckedOutMachineName (""), TransformationVersionGUID (Guid.NewGuid()) and then save the new properties

        TransformationInfoProvider.SetTransformation(ti);
0 votesVote for this answer Mark as a Correct answer

kentico guy answered on August 5, 2021 16:26

Also, I think you need to restart the application after you run that so tack this before the result returns CMS.Helpers.SystemHelper.RestartApplication();

0 votesVote for this answer Mark as a Correct answer

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