Can I Use This Query to Check Every Object in That is Checked Out (Safely)? How to Check in Every P

kentico guy asked on July 12, 2021 20:22

I have been searching for a way to check in every single page template that is checked out. I found the CMS_ObjectSettings table and found something really promising that I'd like to try. I just want to make sure this isn't going to break anything before I do it.

so here's my query to do it:

UPDATE CMS_ObjectSettings
set ObjectCheckedOutByUserID = NULL , ObjectCheckedOutWhen = NULL, ObjectCheckedOutVersionHistoryID = NULL
where ObjectCheckedOutByUserID != NULL AND ObjectSettingsObjectType = 'cms.pagetemplate'

I noticed also that after I set these to NULL I need to go system > restart application before the frontend actually shows it checked back in. But this seems to have worked when I just tested it. It even preserved all of the old version histories.

Will this work to check in every page template? Or am I going to break something unexpected?

Correct Answer

kentico guy answered on August 3, 2021 01:51

final code is:

[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;
        }
        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 July 13, 2021 06:48

When you do a direct changes in the DB, the Kentico admin app has no idea you are changing something. Kentico has certain data loaded into the memory hash tables. So, after you change values in the DB, you need to restart the app so new values are loaded. In general, unless you are 100% sure and you really know what you are doing, changing data in the DB is not supported, recommend and it is really dangerous and can cause serious data inconsistency or data loss. It has never been tested doing it this way so I cannot tell what could be the consequences and results.

I would rather recommend using the API. Get the data class info of given object and set the "CheckedOutByUserID" property accordingly.

And I would maybe re-consider whether you need to use the check in/out of objects when you then want to override it in the DB directly. Wouldn't it be easier just to disable the "Use check-in/check-out for objects" setting?

0 votesVote for this answer Mark as a Correct answer

kentico guy answered on July 13, 2021 17:05

Juraj, what happens if I disable the "use check-in/check-out" when the pages are already checked out?

0 votesVote for this answer Mark as a Correct answer

kentico guy answered on July 13, 2021 22:42 (last edited on July 14, 2021 00:51)

To answer the question above, nothing. However, here's what I've got now. I used the API to update and it seems to have worked aswell. What do you think Juraj?

   protected void Page_Load(object sender, EventArgs e)
{
    var templates = PageTemplateInfoProvider.GetTemplates();

    foreach (var template in templates)
    {
        PageTemplateInfo temptemp;
        if (template.ObjectSettings.ObjectCheckedOutByUserID != 0)
        {
            temptemp = template;
            temptemp.ObjectSettings.ObjectCheckedOutByUserID = 0;
            temptemp.ObjectSettings.ObjectCheckedOutVersionHistoryID = 0;
            temptemp.ObjectSettings.SetValue("IsCheckedOut", false);
            temptemp.ObjectSettings.SetValue("IsCheckedOutByUserID", 0);
            PageTemplateInfoProvider.SetPageTemplateInfo(temptemp);
        }
    }
}

I created a page that's called "Check all items back in" and this code runs when I click on it. but I'm planning on putting this code into a scheduled task that's disabled, so that any dev can click run it to check stuff back in if the other dev is offline and not in the app.

Thoughts?

EDIT: switched does note equal null to does not equal 0 because it's an int not a bool. although this code seemed to work anyway with != null

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on July 14, 2021 08:39

The code looks OK. What I meant was to consider the need of using the check in/out for objects if you need to do this extra hack? It looks like the users are not following maybe some internal editing/developing rules anyway. So, wouldn't it be easier not to use check in/out at all? Or, educate them to be precise and check in the changes after the work is done...not sure how you will handle if someone has half job done and you will basically discard those changes or maybe you will check unfinished job in.

0 votesVote for this answer Mark as a Correct answer

kentico guy answered on August 5, 2021 16:28

I forgot to mention that the objects won't appear checked back in until you restart the application so this needs to go right before the result returns or else the objects won't show as being checked back in, even though they are 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.