Archive for the ‘.NET’ Category

.NET Blogger API Tutorial

Thursday, January 29th, 2009

Yesterday, I found a cool .NET API offered by Google. The Blogger Developer’s Guide for .NET is a cool way to dynamically link your Blogger posts within your own website.

I was a bit frustrated by the documentation (both on the website and within the .NET-generated docs included with their code), so I have decided to write my own tutorial.

Requirements

  • You need to know .NET (either C# or VB.NET)
  • You need to be using some version of Visual Studio. I used Visual Web Developer (Express)

Step One

First, you’re going to need to download and install the API code. Visit this page and download the MSI file. Save and run the file.

Step Two

Next, open your website (or application, etc) in Visual Studio. Pick a page where you want your blog posts to display, and paste the following code into the ASPX page (not the code-behind file).

<div id="blogger" runat="server" />

Step Three

In order to use the Google API libraries, we need to include them in our application. Under Website – Add Reference, click the Browse tab and navigate to C:\Program Files\Google\Google Data API SDK\Redist\ and include the files Google.GData.Client.dll and Google.GData.Extensions.dll. You’ll notice that these files are now added to your \Bin\ folder.

Note: Be sure to upload the \Bin\ directory when you post the code to your web server.

Step Four

After you’ve created the above HTML element (a DIV tag), open the code-behind file. We’re going to add the following code (VB.NET) so we have access to the libraries we need:

Imports Google.GData.Client
Imports System.Net
Imports System.Xml
Imports System.IO
Imports System.Text.RegularExpressions

Step Five

Finally, we’re going to add the following code to actually do the work for us:

   Sub Page_Load() Handles Me.Load

       Dim bloggerHtml As String = ""

       Try
           Dim feedQuery As FeedQuery = New FeedQuery

           Dim blogId As String = "0000000000000000000"
           feedQuery.Uri = New Uri("http://www.blogger.com/feeds/" + blogId + "/posts/default")
           feedQuery.NumberToRetrieve = 5

           Dim googleService As Service = New Service("blogger", "XXXXXXXXXX")
           Dim atomFeed As AtomFeed = googleService.Query(feedQuery)

           Try
               bloggerHtml += "<ul>"

               Dim entry As AtomEntry

               For Each entry In atomFeed.Entries
                   bloggerHtml += "<li><a href='" & _
                       entry.AlternateUri.ToString & _
                       "' target='_blank'>" & entry.Title.Text & _
                       "</a></li>"
               Next

               bloggerHtml += "</ul>"

           Catch feedEx As Exception
               bloggerHtml = "<p>An error occurred while retrieving the Atom Feed from Blogger.</p>"
           End Try

       Catch ex As Exception
           bloggerHtml = "<p><strong>Problem with Blogger API: </strong>" & ex.Message & "</p>"
       End Try

       blogger.InnerHtml = bloggerHtml

   End Sub

This function will run as the page loads, and place HTML code inside of the DIV tag (id=”blogger”) we created in step two.

Be sure to replace the blogId variable (“0000000000000000000″) with your own Blogger id. You can find this by looking at the source code of your blog — it’s used in several of the first lines. You should also replace the “XXXXXXXXXX” with the name of your company/website/project. Honestly, I don’t know if it really matters what you put in this string… the documentation was sketchy on the details.

Your website should now display the last 5 blog posts from your Blogger account!

Awesome N-Tier Tutorial in .NET

Tuesday, January 20th, 2009

I was poking around today to see if I could find a good tutorial which highlights N-tier programming in .NET. I found a really good series of tutorials written by Imar Spaanjaars which I think anyone interested in the subject should read.

The tutorials are well-written, complete with download-able source code, and (most importantly) easy to follow.

Free Version of Visual Studio

Monday, December 15th, 2008

A little more than 3 months ago I downloaded the 90-day free trial of Microsoft Visual Studio so that I could start fooling around with both .NET and Silverlight. I have to say that I am a big fan of Visual Studio, though I’m certainly not a fan of its high price tag to buy a licensed copy.

Last week, I opened Visual Studio and was warned that my trial would expire in 4 days. I’m not done working on a Silverlight tutorial, so I was a little frightened that VS would kick me out… and I decided to just leave the application open and running.

More than a week later, Visual Studio still hasn’t kicked me out.

Now, since I’m really only using VS to test Silverlight I’m not terribly worried about breaking any user-agreement which might be part of the 90-day trial. Plus, I’m not using it in a corporate environment.

The bottom line is this: Microsoft dropped the ball here. While it happens to work out to my advantage, you’d think they would know how to properly program a trial version of their flagship software product.

Generating an email in VB.NET

Tuesday, December 9th, 2008

So I’ve been fooling around lately with .NET, and I have decided that I’m going to redesign my website (or at least create a sub-domain) in .NET as an exercise in developing my skills. I’ll be using Visual Web Developer… mainly because I don’t want to pay for Visual Studio :-)

Since one of the more important parts of the site is the “Contact Me” form, I decided that starting on that part would really be a good example for learning VB.NET.

My current website uses Classic ASP (with VB Script) to generate an email… so how hard could using VB.NET to send an SMTP email really be?

…apparently really hard, particularly since I’m new.

After searching Google for a bit, I stumbled across this blog by Mike Pope. This turned out to be my best friend, although his code didn’t exactly work out-of-the-box. With a few modifications, I had this working in about an hour — not exactly bad for my first VB.NET page!

Here’s what I came up with. I pass the page a handful of variables via a POST operation.

Partial Class contactpage
    Inherits System.Web.UI.Page

    Protected Sub contactpage_load() Handles Me.Load

        Dim email As New System.Net.Mail.MailAddress(Request("Email"))

        Dim Fname As New String(Request("Firstname"))
        Dim Lname As New String(Request("Lastname"))
        Dim phone As New String(Request("Phone"))
        Dim comments As New String(Request("Comments"))

        Response.Expires = -1
        Response.ContentType = "text/plain"

        ' ======================
        ' Send Email to visitor
        '=======================
        Dim visitorSubj As String = "Thank you for contacting aKa Web Design"

        visitorBody += "Thank you for contacting aKa Web Design!"

        Dim noReplyAddress As New System.Net.Mail.MailAddress("AN EMAIL ADDRESS HERE")

        SendMail(noReplyAddress, _
                 email, _
                 visitorSubj, _
                 visitorBody)

        ' **********************
        ' send message to admin
        ' **********************
        Dim sendMessage As String = "Some message here"

        Dim akaWebAddress As New System.Net.Mail.MailAddress("AN EMAIL ADDRESS HERE")

        SendMail(email, _
                 akaWebAddress, _
                 "Contact aKa Web Design", _
                 sendMessage)

    End Sub

    Protected Sub SendMail(ByVal strFrom As System.Net.Mail.MailAddress, _
                               ByVal strTo As System.Net.Mail.MailAddress, _
                               ByVal strSubj As String, _
                               ByVal strMsg As String)

        Dim mailMessage As New System.Net.Mail.MailMessage(strFrom, strTo)
        mailMessage.Subject = strSubj
        mailMessage.Body = strMsg
        mailMessage.IsBodyHtml = True

        Dim mailSender As New System.Net.Mail.SmtpClient()

        mailSender.Host = "YOUR OUTGOING SMTP SERVER HERE"

        mailSender.Port = 25 'YOUR PORT MIGHT BE DIFFERENT

        mailSender.Credentials = New Net.NetworkCredential("YOUR USERNAME", "YOUR PASSWORD")

        Try
            mailSender.Send(mailMessage)
            Response.Write("Message sent!")
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

    End Sub

End Class

I’m happy I finally got it working! My biggest problem was trying to figure out my outgoing SMTP port (I used a shared-hosting account).

I hope this helps someone! Stay tuned for the redesign, coming soon!