Skip to content

Instantly share code, notes, and snippets.

@darrenferguson
Last active February 28, 2017 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenferguson/ec5e4fe681e6403ab35e63abcfb1aa02 to your computer and use it in GitHub Desktop.
Save darrenferguson/ec5e4fe681e6403ab35e63abcfb1aa02 to your computer and use it in GitHub Desktop.
public class Archive
{
public int Id { get; set; }
public string Url { get; set; }
public string Xml { get; set; }
}
public static class PublishedContentExtensions
{
public static void Archive(this IContent content)
{
var h = new UmbracoHelper(UmbracoContext.Current);
var c = h.TypedContent(content.Id);
if (c != null)
{
var u = c.Url;
var xml = content.ToXml();
var archive = new Archive();
archive.Url = u;
archive.Id = c.Id;
var r = xml.CreateReader();
r.MoveToContent();
archive.Xml = r.ReadOuterXml();
var db = ApplicationContext.Current.DatabaseContext.Database;
db.BeginTransaction();
var existing = db.SingleOrDefault<Archive>("select * from Archive where Url = @0", archive.Url);
if (existing == null)
db.Insert(archive);
else
{
db.Execute("update Archive set Xml = @0 where Url = @1", archive.Xml, archive.Url);
}
db.CompleteTransaction();
var dirty = false;
if (content.HasProperty("archived") && !content.GetValue<bool>("archived"))
{
content.SetValue("archived", true);
dirty = true;
}
if (!string.IsNullOrEmpty(content.Name) && dirty)
ApplicationContext.Current.Services.ContentService.Save(content);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment