Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Unlock files when deleting a user
<event name="user:deleted">
<handler type="Helixbase.Feature.ItemUnlock.Events.RemovedUserEventHandler, Helixbase.Feature.ItemUnlock" method="OnUserDeletedUnlockFiles" />
public class RemovedUserEventHandler
public void OnUserDeletedUnlockFiles(object sender, EventArgs args)
if (EventDisabler.IsActive)
var objList = new List<Item>();
var userName = Event.ExtractParameter<string>(args, 0);
Assert.IsNotNullOrEmpty(userName, "User name was null or empty");
var lockedItems = Client.ContentDatabase.SelectItems($"search://*[@__lock='%{userName}%']");
if (lockedItems == null || !lockedItems.Any())
foreach (var lockedItem in lockedItems)
objList.AddRange(lockedItem.Versions.GetVersions(true).Where(version =>
string.Compare(version.Locking.GetOwner(), userName, StringComparison.OrdinalIgnoreCase) == 0));
ProgressBox.Execute(nameof(RemovedUserEventHandler), "Unlocking items", "Network/16x16/lock.png",
UnlockAllItems, "lockeditems:refresh", Context.User, objList);
SheerResponse.Alert($"Successfully unlocked {objList.Count} item(s) checked out by {userName}",
private static void UnlockAllItems(params object[] parameters)
Assert.ArgumentNotNull(parameters, nameof(parameters));
if (!(parameters[0] is List<Item> parameter))
var job = Context.Job;
if (job != null)
job.Status.Total = parameter.Count;
foreach (var lockedItem in parameter)
job?.Status.Messages.Add(Translate.Text("Unlocking {0}", (object)lockedItem.Paths.ContentPath));
if (job != null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.