Blog 4 umbraco 2.0.16 – Ping services

Wednesday, December 16, 2009 by Administrator

Today is about getting an automated pinger setup on our blog so you can notify indexers like Google Blogsearch about your new posts.

We will set it up with a simple event handler that checks if the blog has any urls to ping. We will add the urls to the blog root itself by using the related links control:

image

This creates a simple xml structure which is saved on our document. So everytime we publish a new blogpost, we will simple look up the serviceUrls property recursively and ping all the urls in there.

First, a little snippet to get the value recursively:

private string GetValueRecursively(string alias, int nodeId)
{
    Document n = new Document(nodeId);
    Property p = n.getProperty(alias);
    if (p != null && !string.IsNullOrEmpty(p.Value.ToString()))
        return p.Value.ToString();
    else if (n.Parent != null)
        return GetValueRecursively(alias, n.Parent.Id);

    return string.Empty;
}

So now we can just pass the alias of the property and the Id of the document where we should start looking. If nothing is found it will continue up the content tree untill it finds anything or runs out of parent documents to look in.

As soon as we've done this, we will query the related links xml. It is structure with a parent <links> element containing child <link> elements with attributes on storing urls, names and other meta data. We will also setup the blog name and url which will be send off to the pinged urls.

string blogName = GetValueRecursively("blogName", sender.Id);
string blogUrl = umbraco.library.NiceUrlFullPath(sender.Id);
string currentDomain = HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToLower();

if (!UmbracoSettings.UseDomainPrefixes)
    blogUrl = "http://" + currentDomain + blogUrl;

foreach (XmlNode link in xd.SelectNodes("//link [@type = 'external']"))
{
    string ping = link.Attributes["link"].Value;
    //Log.Add(LogTypes.Debug, sender.Id, ping + " n:" + blogName + " u:" + blogUrl);
    PingService(ping, blogName, blogUrl);
}

This is all put together in an event handler that will fire off after any documents with the contenttype alias = "BlogPost" has been published. The actual ping is handled in the method PingService which simply puts together a chunk of xml and sends it off to the url as a normal HttpWebRequest.

 

Done, we can now ping any service that supports the xmlrpc blog pings.

0 comment(s) for “Blog 4 umbraco 2.0.16 – Ping services”

    Leave a comment