Skip to content

Instantly share code, notes, and snippets.

@darrenferguson darrenferguson/Archive.cs
Last active Feb 28, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.