Simple.Data to Update Orchard Blog Posts Contents

Simple.Data to Update Orchard Blog Posts Contents

I am a huge fan of Amazon Web Services. I use a number of the services for my own businesses as well as the businesses of my clients. I also play around with it a lot on my blogs and various play websites, too, since Amazon offers a free tier. On one of my Orchard Blogs I was using the Amazon S3 Services to store photos, but was recently notified that the free tier period was expiring. I decided to close that account and needed a quick and dirty solution to update the image links in all of my Orchard Blog Posts to point locally to my server instead of Amazon S3. Here is where something like Simple.Data can easily open the Orchard Database, read the contents of each Orchard Blog Post, and update each blog post with new links to the images on the local server. It took me 10 minutes to complete this in Simple.Data, and about 30 minutes to write this blog post!

Simple.Data

If you haven't played with Simple.Data and you are looking for a simple data access layer solution for your .NET websites, I recommend you give Simple.Data a try. It is on Nuget, but you can also find it on GitHub. I even wrote a blog post on Simple.Data awhile ago - Simple.Data for Microsoft .NET Data Access Layer. This is a perfect solution for simple forms-over-data ASP.NET Websites. It doesn't require a lot of set-up and if you work with the conventions you won't need to write a lot of code either. I use Simple.Data quite a bit for utilities when I need to perform data access in a quick manner and usually on a one-time basis.

Updating Orchard Blog Posts with Simple.Data

The Orchard.Blogs Module uses the BodyPart to store the contents of each blog post. Unfortunately, there are a lot of Orchard Modules that use the BodyPart to store their contents, including Orchard Pages. This doesn't matter, however, since only the blog posts linked to Amazon S3 and therefore I could read and update all records in the Common_BodyPartRecord Table in Orchard. The Text Column holds the text of the Orchard Blog Posts and using either Regex, or a quick and dirty String.Replace, I could easily replace the link to Amazon S3 for a link to my local web server.

I have left out the exact names of the strings, but here is the entire application to update the Orchard Blog Posts using Simple.Data. Again, this took me about 10 minutes to write and execute and performance was not a factor.

var db = Database.Open();

var posts = db.Common_BodyPartRecord.All();

foreach(var post in posts) {
    if (post.text != null) {
        post.text = post.text.Replace("{amazon s3 snippet}",
"{local web server snippet}");
        db.Common_BodyPartRecord.UpdateById(Id: post.Id,
Text: post.text);
    }
}

Other than the ConnectionString setting in the web.config file, this is all the code that I wrote in a simple C# Console Application that updated all the blog posts in my Orchard Website. I didn't delcare any custom classes or provide any metadata about the Common_BodyPartRecord Table in the Orchard CMS Database. Simple.Data figured it out and made the update happen - short and sweet.

Conclusion

I highly recommend you don't try this unless you are a professional Orchard Web Developer as there are all kinds of things that can go wrong. However, if you need to update your Orchard Website in a pretty simple manner from a separate .NET application, it is difficult to beat the simplicity of Simple.Data as a data access layer solution. I do realize this could have been done using just SQL Server, but I prefer to use .NET where I have full access to the debug and testing tools. I also wanted to use Simple.Data so I could continue to get more and more comfortable with it. Not bad for 10 minutes of effort :)