MOHAMED JACKARIAH

MOHAMED JACKARIAH

  • 1.6k
  • 181
  • 33.4k

Un Publish and Archive Sitecore items

May 17 2018 10:41 AM
I created custom page to Un Publish and Archive Sitecore items. Sometimes the pages are not unpublish and archive sometimes its working as expected. can anyone help to sortout this issues.
 
Un Publish code:
private bool UnPublishItems(string ItemId, Database webdb)
{
Item item, masterItem = null;
ID itemID = null;
bool unPublished = false;
if (!string.IsNullOrEmpty(ItemId) &&
System.Text.RegularExpressions.Regex.IsMatch(ItemId, @"^[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
itemID = new ID(ItemId);
item = webdb.GetItem(itemID);
if (item != null)
{
masterItem = GetItemById(ItemId);
Database[] databases = new Database[1] { webdb };
using (new SecurityDisabler())
{
masterItem.Editing.BeginEdit();
masterItem.Publishing.NeverPublish = true;
masterItem.Fields[Sitecore.FieldIDs.Workflow].Value = string.Empty;
masterItem.Fields[Sitecore.FieldIDs.WorkflowState].Value = string.Empty;
masterItem.Fields[Sitecore.FieldIDs.Lock].Value = "<r />";
masterItem.Editing.EndEdit();
Sitecore.Handle publishHandle = Sitecore.Publishing.PublishManager.PublishItem(masterItem, databases, webdb.Languages, false, false);
}
unPublished = true;
}
}
return unPublished;
}
 
 Archive Code:
private bool ArchiveItems(string item)
{
bool Archive = false;
Database Masterdb = Sitecore.Configuration.Factory.GetDatabase("master");
using (new SecurityDisabler())
{
Sitecore.Data.Archiving.Archive archive = Sitecore.Data.Archiving.ArchiveManager.GetArchive("archive", Masterdb);
if (archive != null)
{
if (System.Text.RegularExpressions.Regex.IsMatch(item, @"^[{(]?[0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$", System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
Sitecore.Diagnostics.Assert.IsNotNull(item, "item");
Item i = GetItemById(item);
if (i != null)
{
archive.ArchiveItem(i);
Archive = true;
}
}
}
}
return Archive;
}