Unlock Items

Unlock Items

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

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

Unlock Items C# Web Control Code

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

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Sitecore.Data.Items.Item ancestor = db.GetItem("/sitecore/content/home/developers/contentitems");
if (ancestor == 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(); //Div

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);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

if (ancestor.Access.CanWriteLanguage() && ancestor.Locking.IsLocked())
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
ancestor.Editing.BeginEdit();
try
{
if (ancestor.Locking.Unlock())
output.Write("{0}", "Content Item was unlocked");
}
catch (System.Exception ex)
{
Sitecore.Diagnostics.Log.Error("Unlocking item failed. " + ex.Message, this);
ancestor.Editing.CancelEdit();
throw (ex);
}
ancestor.Editing.EndEdit();
}
}
else
output.Write("{0}", "Content Item is not locked");

output.RenderEndTag(); //Div

Notes

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