Checking in all objects
At some point, you may need a script that dynamically checks-in all checked-out objects. For Kentico 11 instances, this script should do the trick!
There may be cases where you’d like to check-in all of your pages and objects while using content locking and object locking. For example, it is recommended to have all objects checked-in before performing an upgrade. The following code will loop through all objects which support object locking and check them in.
Note: When this code is run, objects that are checked-out which contain changes will not be saved, and will be reverted to their state before being checked-out. You should inform users to manually check-in any objects which contain changes that you’d like to save.
To use this code, you can create a custom web part or place it in an ASPX template or web form. Please make sure this is placed in a secure location where it can't be clicked by unathorized users. In the .ascx or .aspx file, add the following code:
<%@ Register TagPrefix="cms" TagName="AsyncLog" Src="~/CMSAdminControls/AsyncLogDialog.ascx" %>
<asp:Button runat="server" Text="Check in all" OnClick="btnCheckIn_Click" />
<asp:Panel Visible="false" runat="server" ID="pnlLog">
<cms:AsyncLog ID="ctlAsyncLog" PostbackOnFinish="false" runat="server" ProvideLogContext="true" />
</asp:Panel>
In the code-behind, add the code that will run when the button is clicked:
protected void btnCheckIn_Click(object sender, EventArgs e)
{
pnlLog.Visible = true;
ctlAsyncLog.EnsureLog();
ctlAsyncLog.RunAsync(CheckInAllObjects, WindowsIdentity.GetCurrent());
}
private void CheckInAllObjects(object parameter)
{
var objectTypes = ObjectTypeManager.AllObjectTypes;
List<string> processTypes = new List<string>();
processTypes.Add("cms.document");
foreach (string ot in objectTypes)
{
var typeInfo = ObjectTypeManager.GetTypeInfo(ot);
if (typeInfo.SupportsLocking)
{
processTypes.Add(ot);
}
}
ctlAsyncLog.AddLog($"Checking in objects from {processTypes.Count()} types..");
foreach (string t in processTypes) CheckInObjects(t);
ctlAsyncLog.AddLog("Done!");
}
private void CheckInObjects(string type)
{
try
{
ctlAsyncLog.AddLog($"Processing {type}..");
var oq = new ObjectQuery(type);
IEnumerable<BaseInfo> objects = oq.TypedResult;
foreach (var baseInfo in objects)
{
if (type.Equals("cms.document"))
{
if(baseInfo.GetIntegerValue("NodeSKUID", 0) > 0)
{
var node = baseInfo as SKUTreeNode;
node.SKU = SKUInfoProvider.GetSKUInfo(node.NodeSKUID);
if (node.IsCheckedOut)
{
node.CheckIn();
node.Update();
}
}
else
{
var node = baseInfo as TreeNode;
if (node.IsCheckedOut)
{
node.CheckIn();
node.Update();
}
}
}
else
{
if (baseInfo.ObjectSettings.ObjectCheckedOutByUserID > 0)
{
baseInfo.ObjectSettings.ObjectCheckedOutByUserID = 0;
baseInfo.Update();
}
}
}
}
catch (Exception e)
{
ctlAsyncLog.AddError(e.Message);
}
}
Disclaimer: This article contains code that has not been tested by our QA staff. You should consider it proof of concept and make sure you test it first to ensure it meets your needs. We also strongly recommend creating a backup of your database before running any custom code.