Unlock All Items

Unlock All Items

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Database
  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Diagnostics.Log
  • Sitecore.SecurityModel.SecurityDisabler
  • System.Exception

Unlock All Items C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

Sitecore.Data.Items.Item home = Sitecore.Context.Database.GetItem("/sitecore/content/Home");
if (home == null) return;

Sitecore.Data.Items.Item[] descendants = home.Axes.GetDescendants();
if (descendants == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

int i = 0;
foreach (Sitecore.Data.Items.Item descendant in descendants)
{
if (descendant.Access.CanWriteLanguage() && descendant.Locking.IsLocked())
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
descendant.Editing.BeginEdit();
try
{
i++;
if (descendant.Locking.Unlock())
output.Write("{0}", descendant.Paths.Path + " was unlocked<br/>");
else
output.Write("{0}", descendant.Paths.Path + " could not be unlocked<br/>");
}
catch (System.Exception ex)
{
Sitecore.Diagnostics.Log.Error("Unlocking item failed. " + ex.Message, this);
descendant.Editing.CancelEdit();
}
descendant.Editing.EndEdit();
}
}
}
if (i == 0)
   output.Write("{0}", "There are currently no items that are locked");

output.RenderEndTag(); //Div

Notes

  • There is no real-time output for this code
  • This control is not bound to this page