Skip to content

Instantly share code, notes, and snippets.

@darrenferguson
Created February 21, 2017 09:27
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/6be53533f6b4d614702b2f0bcfa04737 to your computer and use it in GitHub Desktop.
Save darrenferguson/6be53533f6b4d614702b2f0bcfa04737 to your computer and use it in GitHub Desktop.
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();
if (content.HasProperty("archived") && !content.GetValue<bool>("archived"))
{
content.SetValue("archived", true);
if(!string.IsNullOrEmpty(content.Name))
ApplicationContext.Current.Services.ContentService.Save(content);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment