love to code .net

blogging mostly about one of my favorite subjects, programming

Month List


BlogEngine.NET ignores Windows Live Writer post date

I started trying to automate future posts by specifying a post date that was in the future.  Directly posting through the BlogEngine.NET web interface works without a hitch.  When using Windows Live Writer though the post is posted using the current date and time, making it immediately visible.  I found the cause of this problem was in the BlogEngine.Core.API.MetaWeblog class.  The particular problem is that WLW doesn't provide pubDate, instead it provides dateCreated.  Here is the code that needs to be changed that can be found in the GetPost method of the MetaWeblog class.

1: if (node.SelectSingleNode("value/struct/member[name='pubDate']") != null)
2: {
3:   try
4:   {
5:     string tempDate = node.SelectSingleNode("value/struct/member[name='pubDate']").LastChild.InnerText;
6:     temp.postDate = DateTime.ParseExact(tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
7:   }
8:   catch
9:   {
10:     // Ignore PubDate Error
11:   }
12: }

In addition to adding dateCreated as a possible source of the postDate, I decided to remove the reason to catch the exception, as exceptions are costly.  Luckily DateTime provides a TryParseExact method to accomplish this.  Here is the updated section.

1: if (node.SelectSingleNode("value/struct/member[name='pubDate']") != null ||
2:     node.SelectSingleNode("value/struct/member[name='dateCreated']") != null)
3: {
4:     string tempDate = (node.SelectSingleNode("value/struct/member[name='pubDate']") ?? node.SelectSingleNode("value/struct/member[name='dateCreated']")).LastChild.InnerText;
5:     DateTime.TryParseExact(tempDate, "yyyyMMdd'T'HH':'mm':'ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out temp.postDate);
6: }

Notice that on line 5, I used the null coalescing operator as I did in this post.  Personally I think this C# feature is under used.  Here is the code file.  The code in this post is released under the Microsoft Reciprocal License, which is the license BlogEngine.NET uses.

kick it on DotNetKicks.com

Comments are closed