Created
February 21, 2017 09:27
-
-
Save darrenferguson/6be53533f6b4d614702b2f0bcfa04737 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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