Translate

Tuesday, 15 July 2014

Blogger post from ASP.Net

Sample post! It looks like setting a draft post to false on the AtomEntry object does not work or it is simply a matter of providing the postId alongwith the blogId and not setting the URL to default feeds.

Below is the code: (you can use NUGet to install the Google API into your VS project)

using System;
using Google.GData.Client;
using System.Web.UI;

namespace BloggerPost
{
    public partial class _Default : Page
    {
        string blogId = ""; // This id will be part of the query string in your browser address bar when you access your blog
        string username;
        string password;
        private void PostOrUpdateBlogPost(string yourBlogId,bool isUpdate)
        {
            if (yourBlogId != null)
            {
                var service = new Google.GData.Client.Service("","Application Name");
                service.Credentials = new GDataCredentials(username, password);
                AtomEntry newPost = null;
                if (isUpdate)
                {
                    try
                    {
                        // need to correct the URL here
                        newPost = service.Get(yourBlogId);
                        newPost.Categories.Clear();
                        // Setting the isDraft property requires the above URL to contain the postId (need to confirm test this).
                        if (newPost.IsDraft) newPost.IsDraft = false;
                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    newPost = new AtomEntry();
                    newPost.Title.Text = "Blogger post from ASP.Net";
                    newPost.Content = new AtomContent();
                    newPost.Content.Content = "Sample post! It looks like setting a draft post to false on the AtomEntry object does not work or it is simply a matter of providing the postId alongwith the blogId and not setting the URL to default feeds.";
                    newPost.Content.Type = "html";
                }
             
                try
                {
                    if (!isUpdate)
                    {
                        Uri blogPostUri = new Uri("http://www.blogger.com/feeds/" + yourBlogId + "/posts/default");
                        newPost = service.Insert(blogPostUri, newPost);
                    }
                    else
                    {
                        newPost = newPost.Update();
                    }
                }
                catch (Exception ex)
                {              
                }
            }
        }
 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PostOrUpdateBlogPost(blogId,false); // the boolean is to specify if it is a new post or an update.
            }
        }
 
    }
}

Publishing a post using the Google API for Blogger!

It is not as easy as many have posted on the web! Many posts indicate that all you need to do

is send in your credentials to the service object. Not as simple as that !

Blogger API Post


This post was created with the Blogger API v3

Tuesday, 17 June 2014

Two singularly complex problems - Arrays and Lists

The delightful aspect of programming in C# is the large number of possibilities, to resolve a problem, available to a programmer in the form of different forms of implementations across versions of the underlying technology and framework.

It is delightful because two different programmers may provide a solution to the same problem, without differing in technique, because the implementation of the same language feature across the different versions of the C# language compiler are different.

It can be torrid, though, if you do not enjoy the intricacies of working with different types in the CLR and/or the underlying unmanaged types of the Windows platform.

1. Unmanaged array - the 'mysterious' object[*]

The Microsoft.Office.Interop,Excel Chart object presents a challenge in the way it exposes the Chart's series collection's data values.

For instance, if you want to read the Chart's Source Data range/Data Table, the problem is in the way the series collection exposes each series' data values. Being a System.ComObject, the series.Values (that represent each Chart Series values) returns an unmanaged array !! What is so surprising, you may ask?

The surprise is that you may convert it to an object array but may inadvertently leave out testing it as it will compile ok. At runtime, though, an error that an object of type [,] cannot be converted to an object of type [] will be thrown!

The exasperating aspect of this error has to be experienced to really appreciate the nature of the error !!

The reason it is exasperating is because you may try creating an Array with CreateInstance and while it should work, at a glance,

var array = System.Array.CreateInstance(
                            typeof(object),
                            new int[] { totalColumns },
                            new int[] { totalRows });
array = series.Values;

it does not !

The solution is to iterate through the series.Values as below:

// --- 1. Get ChartObjects in worksheet
//-- 2. iterate through the chart object's series collection

foreach (object seriesValue in series.Values as System.Array)
{
double d = (double)seriesValue;
list.Add(d);
}

The trick is to convert the unmanaged array values into an array while iterating through it !

Best Practice
3. If you are using a System.ComObject in a for... or foreach... loop make sure to release the COM object with Marshal.ReleaseComObject in the System.Runtime.Interopservices namespace in the finally block of a try...catch as below:

foreach (..... ComObject in ...){
try{
....
}
catch{}
finally{
if (object!=null)
Marshal.ReleaseComObject.(...);
}
}

Remember, you cannot set a ComObject used in a loop to null ! A runtime exception will be thrown if you attempt to do so.


2. Compare two Lists or Arrays

The option to obtain the iterated value into an array or a list is as per your requirement.

In both the cases, you may want to work with both the array or the list. 

Below are two techniques to compare, using, first, the TrueForAll Linq method (make sure to add the System.Linq namespace) as below.

                            var list = new System.Collections.Generic.List();
                            var list1 = new System.Collections.Generic.List();

                                            if (list.TrueForAll(list1.Contains) && list1.TrueForAll(list.Contains))
                                            // --

Or, convert the array into a List and use the System.Array's SequenceEqual check for an array,

object[] rangeValueArray = (from item in list select item as object).ToArray();
object[] seriesValueArray = (from item in list1 select item as object).ToArray();

if (rangeValueArray.SequenceEqual(seriesValueArray))
// --
else if (!rangeValueArray.SequenceEqual(seriesValueArray))
// --

This method helps check if the elements are equal plus in sequence !