Rss 2.0 via FEED
Ken Hughes... - CSharp
Productivity, Technology and Automating Everything...
    
 

imageOriginally I had my website set up to default to my blog (so dasBlog was installed at the website root level instead of in a /blog subfolder).

Recently I have been writing some Web Applications and wanted to restructure my website so that there is a subfolder for each app / main area (blog, projects, etc).

The problem being that all the links out there point to http://www.kapie.com/blog/somefile.aspx and these would all be dead link when moved to http://www.kapie.com/blog/somefile.aspx, so I needed to respond with a HTTP 302 to the original links and tell the client that it should temporarily redirect to the new location (changing to a HTTP 301 when I am happy that it is all working).

This called for a HttpModule...

namespace KSL
{
     public class BlogRedirector : IHttpModule
     {
VisualStudioLogo         private EventHandler onBeginRequest; 

         public BlogRedirector()
         {
             onBeginRequest = new EventHandler(this.HandleBeginRequest);
         }
 
         void IHttpModule.Dispose()
         {
         }
 
         void IHttpModule.Init(HttpApplication context)
         {
             context.BeginRequest += onBeginRequest;
         }

         private void HandleBeginRequest( object sender, EventArgs evargs )
         {
             HttpApplication app = sender as HttpApplication;
 
             if ( app != null )
             {
                 HttpContext context = app.Context;
                 string host = app.Request.Url.Host;
                 string requestUrl = app.Request.Url.PathAndQuery;

                 if (requestUrl.Contains(@"/blog/") 
                     || requestUrl.Contains(@"/foo1/") 
                     || requestUrl.Contains(@"/foo2/") 
                     || requestUrl.Contains(@"/foo3/")
                     || requestUrl.Contains(@"/foo4/")
                     || (requestUrl.ToLower() == app.Request.Url.Scheme + @"://" + host + @"/default.aspx"))
                 {
                     return;
                 }
                 else
                 {
                     Uri newURL = new Uri(app.Request.Url.Scheme + @"://" + host + @"/blog" + requestUrl);
                     context.Response.RedirectLocation = newURL.ToString();
                     context.Response.StatusCode = 302;
                     context.Response.End();
                     return;
                 }
             }
         }
    }
}

This basically allows me to redirect any url that does not contain /blog/ or /foo*/ or is not /default.aspx by sending a 302 status code and adding /blog/ to the url to generate the redirect url. The compiled DLLs were stored in the bin folder off the root and the web.config in the root needed the following adding...

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <httpModules>
      <add type="KSL.BlogRedirector,BlogRedirector" name="BlogRedirector" />
    </httpModules>
  </system.web>
</configuration>

This seemed to work fine some of the time, but was a bit hit and miss.... Further investigation pointed me at the web.config files in the subfolders and that they would inherit the sections from the root web.config. So all the subfolder web.config files got the following added.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <httpModules>
      <remove name="BlogRedirector" />
    </httpModules>
  </system.web>
</configuration>
GEO 51.4043197631836:-1.28760504722595
Posted: Wednesday, December 19, 2007 7:09:38 PM (GMT Standard Time, UTC+00:00)  #   Comments [0]
TAGS: .NET | ASP.NET | C Sharp | Development | Web

InsertPanel I have just completed a new Windows Live Writer plugin. This extension allows ease insertion of geo microformat information.

It allows the user to easily choose the location they want to insert (in microformat) from a Virtual Earth map and also configure how it is displayed (if at all on the post.

Recently I (with considerable help from Alexander Groß) added GeoRss support for dasBlog. The co-ordinates can be specified when adding a post via the web interface. This plugin is stage two of this support, stage three will be parsing the geo microformat when a post is added and using that to populate the GeoRss info.

The end goal being to allow the geo info to be entered when creating a post in Writer and having that info available in GeoRss format in the feed.

I started this plugin with the view to using Google Maps, however they require that you get an API key and that key is only valid for a particular web site / URL path. This foiled my plans to embed the map in a Windows Forms WebBrowser control (I did look at producing an html page that was served from my web site, and using it embedded in the WebBrowser - not scaleable and too much configuration for a normal user to do.

InsertMicroformat 

I hadn't looked in depth at Virtual Earth, but recently went to MixUK:07 and saw a couple of demos / presentations on it - a quick look found that it didn't need a API and was not tied to a particular URL / path - the JavaScript for it is pretty similar to the one for Google Maps so learning curve was pretty short. The only issue I have with http://local.live.com is that there is no (currently) facility to enter a GeoRss feed in the search query and just display the data (the current method of displaying this kind of data is to embed a map in your own pages and use their API to display the items as a 'collection').

You can get the installer for it here InsertGeoFormatSetup.msi (325Kb).

GEO 51.4043243116043:-1.28760516643523
Posted: Saturday, September 15, 2007 11:12:11 PM (GMT Daylight Time, UTC+01:00)  #   Comments [5]
TAGS: .NET | ASP.NET | C Sharp | Dasblog | GPS | RSS | Web

I have completed stage one of GeoRss enabling dasBlog.

In the config page I added some options for enabling GeoRss, specifying a default lat/long and enabling ‘integration’ with Google maps. There is also an option to use the default lat/long for any non geocoded posts.

  clip_image0022_thumb1

If GeoRss is enabled then the edit entry screen provides textboxes to allow specifying lat/long (populated with defaults from config page).

clip_image00421_thumb2

If the google maps integration is enabled then you’ll get the ‘Show Map’ button and clicking it will display a map which you can move around until you find the location and then click on the location to get the lat/long texboxes populated.

clip_image00681_thumb1

If you have existing non geocoded posts then you can have the default lat/long added to those if you wish.

I puzzled around for ages when trying to display the georss in google (http://maps.google.com/maps?q=yourfeedaddress) – it kept telling me that the feed was invalid. I eventually found that feedburner was adding <atom10:link blah blah /> to the xml which for some reason google maps thinks is invalid. The only way I could find to prevent feedburner adding the atom link was to turn OFF the ‘Browser Friendly’ feature in feedburner.

So – stage 2...

The work I still want to do with this is basically to add macros to get lat/long  - fairly easy I guess, and then some way to specify lat/long from Windows Live Writer (and other offline blog clients) – a little more complex. Scott mentioned a geo microformat and from my initial looks seems to be a good route to take - watch this space...  

Now it is simply a case of retrofitting the geo info into all my old posts...

 

Posted: Saturday, September 08, 2007 10:33:40 PM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | ASP.NET | C Sharp | Dasblog | Development | GPS | RSS | Software

Some of the code I have for importing data (from ACT! 2000) to MS CRM creates new 'PhoneCall' activities / objects. The problem is, that it seems MSCRM does not allow you to programmatically modify the 'create date'.

Here is the code I use...

phonecall pc

            CrmDateTime start = new CrmDateTime();

            start.Value = DateTime.Parse("10/08/2005 12:30");

            pc.actualstart = start;

            pc.scheduledstart = start;

            CrmDateTime end = new CrmDateTime();

            end.Value = DateTime.Parse("10/08/2005 14:30");

            pc.actualend = end;

            pc.scheduledend = end;

            pc.subject = "Phone call regarding sales of Widgets Q2/2005");
***

            string desc =  "Start    : " + pc.actualstart.Value + "\n";

            desc += "End       : " + pc.actualstart.Value + "\n\n";
***

            desc += "The details of the phone call go in here");

            pc.description = desc;

            pc.regardingobjectid = new Lookup();

            pc.regardingobjectid.type = EntityName.contact.ToString();

            Guid contactGuid = new Guid(guidOfContactWeTelephoned);

            pc.regardingobjectid.Value = contactGuid;

 

 

The actualstart and scheduledstart (and ends) get populated with the current datetime (this seems to happen if the time they are set to is in the past).

 

Note the two lines between the ***'s - this is my solution / workaround and simply include the start/end times as text in the body/description of the phone call object.

 

Posted: Monday, August 20, 2007 2:07:39 PM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | C Sharp | Development | MSCRM | Software

A few months ago I added a 'Daily Report Email' feature to dasBlog, so I could have the activity reports (referrers, searches etc) delivered to my mailbox at the end of every day.
I wanted to take this one step further and deliver it via RSS (and dasBlog is a RSS engine after all).

There were a couple of suggested methods raised on the dasBlog developers mailing list; one was using HTTP authentication - I was reluctant to use this as Outlook 2007 does not support authorized feeds. The other suggestion was to obfuscate the URL with a Guid or the like - this was my preferred option.

So, I have been working on it over the past few evenings, got it rolled out to this blog and it seems to be working pretty well.

image

I added a checkbox to the site configuration page, that when checked generates a Guid that is used as part of the path. The 'activityrss.ashx' page doesn't exist at that path (in fact it doesn't exist at all!), instead there is a HttpHandler for the file 'activityrss.ashx' that intercepts any requests for that file and does some processing.

In this processing I check that the requested URL contains the Guid and if so, generate the Activity RSS feed. The content of each feed item is the activity report (similar format to the daily report email).

image

NOTE: This is not in a release yet, if you want it you'll have to check out the source, patch it with this patch file and compile, deploy it manually.

 

Posted: Saturday, July 21, 2007 12:18:41 AM (GMT Daylight Time, UTC+01:00)  #   Comments [1]
TAGS: .NET | ASP.NET | C Sharp | Dasblog

So after some poor experiences with the MSCRM Data Migration framework I decided to get pragmatic and write a C# app to do the migration.

The CDF is poorly documented at best, it seems they (Microsoft) give you a bunch of database tables, an Excel spreadsheet outlining the schema and a 'Good Luck'. There is little 'googleable' (is that a word) knowledge about it either.

The good news was that the MSCRM SDK is much better documented (on MSDN). There is not a lot of googleable info around but there is enough (Stunnware proved pretty helpful for me).

There were other challenges also - the software we purchased for exporting the ACT! 2000 data to Access (Exporter Pro) did a good job of getting the data out of ACT but the Unique ID left a bit to be desired, they are basically a munge of punctuation characters and alphanumerics - what's wrong with a GUID or a int ??

So, anyway, I got there in the end...

The connecting to the CrmService was pretty easy, as was the population and addition of an account.

            crmSvc = new CrmService();

            crmSvc.Url = MsCrmUrl;

            crmSvc.Credentials = new System.Net.NetworkCredential(CrmUsername, CrmPassword, CrmDomain);

 

            account acct = new account();

            acct.name = "company name";

            acct.address1_line1 = "address line one";

            acct.address1_line2 = "address line two";

            acct.address1_line3 = "address line three";

            // etc

 

            Guid acctGuid = crmSvc.Create(acctGuid);

 

 

Simple as that - do it for each account...

Next installment will outline adding Contacts an then linking them to an account.

 

Posted: Tuesday, July 03, 2007 9:51:25 PM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | C Sharp | MSCRM | Software

I'm in Western Mass (Westboro, MA) again this week and while that normally means 16 hour days (there is nothing much else to do but sit around a hotel room so why not...), this week I decided to work on a personal project for a bit.

Part of it was some code to check a machines public IP address and update it to DynDNS. Luckily they have an excellent developers / API section that explains everything you need to do this. There are two sections to it, detecting the machines public IP address and updating the hostnames associated with your account, both of which they provide a service for.

It's pretty easy to follow and within an hour or so I had come up with a class to do both tasks. Here is a snippet for the 'CheckIP' function:

public static string CheckIP()

{

 

    // check the current public IP address

    string ipString = string.Empty;

    WebClient wc = new WebClient();

    try

    {

        string result = wc.DownloadString("http://checkip.dyndns.com");

        return ParseCheckResult(result);

    }

    catch (WebException)

    {

        ipString = string.Empty;

    }

    return ipString;

}

 

 

Just call a URL (http://checkip.dyndns.com) and it returns your public IP address, there was some parsing of the return text but it is pretty simple. Click the link and see for yourself.

 

Next was the 'UpdateIP' function, here's the snippet:

public static string UpdateIP(string username, string password, string ipaddr, string hosts)

{

    // check the current public IP address against what we want to update to

    string updateurl = "http://members.dyndns.org/nic/update?hostname";

    string result = string.Empty;

    WebClient wc = new WebClient();

 

    string url = string.Format(@"{0}={1}&myip={2}&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG", updateurl, hosts, ipaddr);

    try

    {

        wc.Credentials = new NetworkCredential(username, password);

        wc.Headers.Add("User-Agent", "KSL - WHS Updater - 1.0");

 

It is basically just a case of passing a querystring with all the details to http://members.dyndns.org/nic/update , ensuring you set the credentials to your DynDNS account username and password and specifying a unique User-Agent.

 

The complete project files (including a small 'user' application to test it) can be found here (61K).

Posted: Tuesday, June 19, 2007 2:27:49 AM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | C Sharp | Development | Software | Tools

Just didn't work for me.

I had it all set up as per the docs, imported the data into the CDF database, tried to 'Migrate' and it completed but migrated no records (even though I had 8000+ in the CDF tables).

I tried both the snippets I gleaned from googling :

  • Make user the user does not have 'Restricted Access'
  • Turn off 'Fast Load' mode.

Still no joy. All the 'required' fields are populated, the mapping wizard works correctly, but no 'migration'. It is a real pain as there is nowhere to go next, the documentation about CDF is really poor as is the logging/trace/error output from the Migration Wizard.

Ever the pragmatist, I cracked open VS and and have started on an app which uses the CrmService webservice to import the data. Watch this space for the source when I'm done...

Posted: Friday, June 01, 2007 1:34:41 PM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | C Sharp | Software

One of the projects I am involved with at work was evaluating Microsoft CRM (MSCRM).

Out of the box, it comes as a pretty well fully featured CRM application, but it is also hugely customizable. I downloaded the SDK from here and had a quick play.
Within 20 – 30 minutes I had a quick extension / customisation working – it is a simple webpage allowing customers to register their own details and when submitted, that customer is automatically added to MSCRM.

It was incredibly simple to get working, just :-

  • Create a new web project
  • Add a web reference to MsCrmSdk
  • Add some textboxes and a submit button and on the 'submit click' event just :-
    • Create a new CrmService object
    • Set the URL for it and provide credentials if necessary
    • Create a new contact object
    • Populate it with the data from the textboxes
    • Call 'Create' on the CrmService object passing the contact object as a parameter

You're done...

Below is some sample code – it only picks up a few details about the contact, but should be fairly easy to enhance ... Enjoy

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using MsCrmSdk;

 

    public partial class _Default : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            btnInsert.Click += new EventHandler(btnInsert_Click);

 

        }

 

        void btnInsert_Click(object sender, EventArgs e)

        {

            string salutation = txtSalutation.Text;

            string firstName = txtFirstName.Text;

            string lastName = txtLastName.Text;

            string emailAddress = txtEmailAddress.Text;

 

            CrmService svc = new CrmService();

            svc.Url = "http://10.10.121.226:5555/mscrmservices/2006/crmservice.asmx";

            svc.Credentials = new System.Net.NetworkCredential("kenh", "Exchange1", "kennet");

 

            contact newContact = new contact();

            newContact.salutation = salutation;

            newContact.firstname = firstName;

            newContact.lastname = lastName;

            newContact.emailaddress1 = emailAddress;

 

            Guid guidResult = svc.Create(newContact);

 

            txtSalutation.Text = "";

            txtFirstName.Text = "";

            txtLastName.Text = guidResult.ToString();

 

        }

 

    }

Posted: Friday, May 25, 2007 12:38:31 PM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: ASP.NET | C Sharp | MSCRM

 I use dasBlog as the vehicle/technology for this site and currently have it configured at the root of the website (so you go to http://www.kapie.com/blog and you get directly into this blog). This is kind of an unusual setup as most people put it in a /blog subdirectory or virtual directory.

I have plans to put a fair bit more static content up so I wanted to change to the standard of /blog subfolder - the problem is that all my current links will fail when I move the content along with all the content google has cached / indexed about the site - Not Good.

Scott Hanselman suggested in response to a question I posed on the dasBlog mailing list that writing a HttpModule with a HTTP temporary redirect (HTTP response code 302) might be the way to go and reckoned it might only be a handful of lines of code....

 I was pleasantly surprised at how easy it was...

I have included the code below in case anyone else might find it useful. It is pretty specific at the moment (takes the requested URL and if it does not include /blog/ then it insert it) but it could easily by made generic (RegEx or the like).

Happy to share the actual source files if anyone wants them - drop me a mail/comment. 

using System;

using System.Web;

using System.Text;

 

 

namespace KH

{

    public class Redirector : IHttpModule

    {

        private EventHandler onBeginRequest;

 

        public Redirector()

        {

            onBeginRequest = new EventHandler(this.HandleBeginRequest);

        }

 

        void IHttpModule.Dispose() { }

 

        void IHttpModule.Init(HttpApplication context)

        {

            context.BeginRequest += onBeginRequest;

        }

 

        private void HandleBeginRequest(object sender, EventArgs evargs)

        {

            HttpApplication app = sender as HttpApplication;

 

            if (app != null)

            {

                string host = app.Request.Url.Host;

                string requestUrl = app.Request.Url.PathAndQuery;

                if (requestUrl.IndexOf("/blog/") == -1)

                {

                    Uri newURL = new Uri(app.Request.Url.Scheme + "://www." + host + "/blog" + requestUrl);

                    app.Context.Response.RedirectLocation = newURL.ToString();

                    app.Context.Response.StatusCode = 302;

                    app.Context.Response.End();

                    return;

                }

                else

                {

                    return;

                }

            }

        }

    }

}

 

 

Posted: Thursday, March 08, 2007 8:10:19 PM (GMT Standard Time, UTC+00:00)  #   Comments [0]
TAGS: .NET | C Sharp | Dasblog | Development

In the first version of my GPS Library software (written in VB6) I had a 'Protocol' property that indicated which protocol we were using (for example Garmin, Magellan, NMEA etc). Then, every time we needed to do something at the protocol layer (Sending or Receiving data) we had an if statement :

If gps.Protocol = GpsProtocol.Garmin Then
     ' Do it this way
EsleIf gps.Protocol = GpsProtocol.Magellan Then
     ' Do it that way
Else
     ' Do it the other way

Horrible and messy !! Adding a new protocol meant going back to every function and amending it.

In the new .NET version, it's much more object oriented and I've used a number of software design patterns.

To solve the above problem I used a Factory pattern. The Factory pattern allows you to create objects but let the client decide what type those objects should be. So, I can basically let the client set the Protocol property and then, depending on that setting create a different object for each protocol.

I start with a base class that all the 'codec' classes inherit from, then in the 'creation' (when the client creates the object) I look at the Protocol property and choose which concrete class to return:

GpsCodec is the base class and the concrete classes are GarminCodec, MagellanCodec etc. I have a static method in the base class called 'CreateCodec' that takes a 'Protocol' parameter. So it looks like this

public static GpsCodec CreateCodec(GpsProtocol protocol)
{
     switch (protocol)
     {
          case GpsProtocol.Garmin:
               return new GarminCodec();
               break;
          case GpsProtocol.Magellan:
               return new MagellanCodec();
               break;
          default:
               return null; // error
               break;
     }
}

So now I can simply create a codec class based on the Protocol property and use that for all the communication to / from the device. Adding a new protocol is simply a case of creating the new class and modifying the CreateCodec method - much simpler.

Watch this space for the free .NET GPS Library (including source).

Posted: Sunday, November 12, 2006 3:57:31 PM (GMT Standard Time, UTC+00:00)  #   Comments [0]
TAGS: .NET | C Sharp | Design Patterns | Development | GPS

About 4 or 5 years ago I wrote a GPS ActiveX control in VB6.0.
Although it work really well (and fast), it was difficult to maintain - to be honest it was pretty messy, a large monolith with some real horrible 283654276354 line functions.
I sold it as Kapie Systems for a while and had some good success with it, it was mentioned at a couple of GIS conferences etc...

Anyway, I had been meaning to update it (to .NET) for sometime, but I wanted to do it right, make it easy to extend (to plug-in other GPS protocols) and easy to maintain.

I just got a basic .NET implementation of it working last night (at last !!). It's Garmin propriety protocol only at the moment, but the whole framework is there for extending to other protocols (which I'll be adding shortly).
I also added import and export to GPX format, which is a great portable standard - the GIS / Mapping community has been crying out for this for ages.

I expect to have something releasable in a few weeks (it will be open source) but if anyone wants an early view or wants to discuss my plans ideas then let me know.

Posted: Friday, November 10, 2006 12:00:33 AM (GMT Standard Time, UTC+00:00)  #   Comments [3]
TAGS: .NET | C Sharp | Development | GPS

One of my gripes with dasBlog is the reporting. To get to reports I have to browse to my blog, login and then view the reports online - bit of a pain doing this every day (yes I am so vain that I like to see my stats on a daily basis).

So I spent a few hours recently adding an 'Daily Activity Report' feature.  It was surprising easy - checkout (get a working copy) of the source from sourceforge (browse it here https://svn.sourceforge.net:443/svnroot/dasblogce/trunk), familiarize yourself with it, add your changes and create a patch. Send the patch to someone with admin rights (so it can be checked in) and that's it... I'd recommend to anyone to try it out...

Anyway the 'Daily Activity Report' feature - basically starts a background thread which caches the current date and then ticks every hour. Each tick it will check the current date against the cached date and if they do not match then it will send an email to either the Notification list or the Site Owner / Administrator. It is configured by simply checking a box in the configuration page (in the SMTP Server section)

The email contains similar detail to that of the 'Activity... Referrers' page.

I also made the method / class that implements the creating / sending the report public so that it can be extended - I was thinking along the lines of a 'Send Daily Activity Report Now' button or enhancing the Mail-To-Weblog to monitor for a specific phrase in the subject and if found to send the report (maybe also specify the date)...

Watch this space for updates...

Posted: Wednesday, November 08, 2006 3:02:13 PM (GMT Standard Time, UTC+00:00)  #   Comments [1]
TAGS: .NET | C Sharp | Dasblog | Software

Singleton Design Pattern, is a solution to a common problem in the software domain where you want to create ONE AND ONLY ONE instance of an object/class.

Put simply, it stops client applications creating their own instances of an object and routes all creation of the object through one method. If an instance of the class already exists then that instance is returned, if an instance of the class does not exist then a new instance is created.

So, no matter how many clients access the class there is only ever one instance (that all clients access)

Here's some sample code of how to achieve a Singleton class :

using System;

using System.Collections.Generic;

using System.Text;

 

namespace KenAndSarah.GpsControl

{

    public class GpsControl

    {

        private static GpsControl instance = null;

 

        private GpsControl()

        {

            // do not allow a public constructor

        }

 

        // call this to get (or create) a reference to the class

        public static GpsControl GetGpsControl()

        {

            if (instance == null)

                instance = new GpsControl();

            return instance;

        }

    }

}

 

So to clients would simply use it like this

        GpsControl gps = GpsControl.GetGpsControl();

 

Any subsequent clients who make the same call get a reference to the already created instance of the class.

Posted: Friday, October 06, 2006 3:50:20 PM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | C Sharp | Design Patterns | Development | Software

Here's the code for the macros I created - feel free to copy / compile it yourself. I also included a compiled DLL here - BUT it is compiled against .NET2.0 so will only work if your hosting Dasblog in a ASP.NET2.0 virtual directory.

  • Copy the compiled DLL into your /bin folder
  • Edit your Web.config file, locate the <newtelligence.DasBlog.Macros> node
  • Add the following element <add macro="khmacros" type="KensDasblogMacros.KensMacros, KensMacros"/>
  • Edit your template pages to include <%TrackbackCount()|khmacros%> or <%TrackbackountAll()|khmacros%> or <%KensBlogStats()|khmacros%>

using System;

using System.Collections;

using System.Text;

using System.Web.UI;

using newtelligence.DasBlog.Runtime;

using newtelligence.DasBlog.Web.Core;

using newtelligence.DasBlog.Util;

 

 

namespace KensDasblogMacros

{

    public class KensMacros

    {

 

        protected SharedBasePage sharedBasePage;

        protected Entry currentEntry;

 

        public KensMacros(SharedBasePage page, Entry entry)

        {

            sharedBasePage = page;

            currentEntry = entry;

        }

 

        public virtual Control TrackbackCount()

        {

            if (this.currentEntry != null)

            {

                int trackbackCount = 0;

                IBlogDataService dataService = sharedBasePage.DataService;

                trackbackCount = dataService.GetTrackingsFor(currentEntry.EntryId).Count;

                return new LiteralControl(trackbackCount.ToString());

            }

            return new LiteralControl("");

        }

 

        public virtual Control TrackbackCountAll()

        {

            int trackbackCountAll = 0;

 

            IBlogDataService dataService = sharedBasePage.DataService;

            DateTime[] daysWithEntries = dataService.GetDaysWithEntries(UTCTimeZone.CurrentTimeZone);

 

            foreach (DateTime day in daysWithEntries)

            {

                EntryCollection entries = dataService.GetEntriesForDay(day, UTCTimeZone.CurrentTimeZone, String.Empty, 1, int.MaxValue, String.Empty);

                foreach (Entry potentialEntry in entries)

                {

                    trackbackCountAll += dataService.GetTrackingsFor(potentialEntry.EntryId).Count;

                }

                return new LiteralControl(trackbackCountAll.ToString());

            }

            return new LiteralControl("");

        }

 

        public virtual Control KensBlogStats()

        {

            StringBuilder sb = new StringBuilder();

 

            try

            {

                IBlogDataService dataService = sharedBasePage.DataService;

 

                DateTime monthFirst = new DateTime((DateTime.UtcNow.Year), (DateTime.UtcNow.Month), 1, 0, 0, 0);

                DateTime monthLast = new DateTime((DateTime.UtcNow.Year), (DateTime.UtcNow.Month), DateTime.DaysInMonth(DateTime.UtcNow.Year,DateTime.UtcNow.Month),23,59,59);

 

                DateTime weekFirst = Macros.GetStartOfCurrentWeek();

                DateTime weekLast = Macros.GetEndOfCurrentWeek();

 

                DateTime yearFirst = Macros.GetStartOfYear(DateTime.UtcNow.Year);

                DateTime yearLast = Macros.GetEndOfYear(DateTime.UtcNow.Year);

 

                int allEntriesCount = 0;

                int yearPostCount = 0;

                int weekPostCount = 0;

                int monthPostCount = 0;

                int trackbackCount = 0;

                DateTime[] daysWithEntries = dataService.GetDaysWithEntries(newtelligence.DasBlog.Util.UTCTimeZone.CurrentTimeZone);

                foreach (DateTime day in daysWithEntries)

                {

                    EntryCollection entries = dataService.GetEntriesForDay(day, newtelligence.DasBlog.Util.UTCTimeZone.CurrentTimeZone, String.Empty, 1, int.MaxValue, String.Empty);

                    allEntriesCount += entries.Count;

                    foreach (Entry potentialEntry in entries)

                    {

                        if (potentialEntry.CreatedUtc > monthFirst && potentialEntry.CreatedUtc <= monthLast)

                        {

                            monthPostCount++;

                        }

 

                        if (potentialEntry.CreatedUtc > weekFirst && potentialEntry.CreatedUtc <= weekLast)

                        {

                            weekPostCount++;

                        }

 

                        if (potentialEntry.CreatedUtc > yearFirst && potentialEntry.CreatedUtc <= yearLast)

                        {

                            yearPostCount++;

                        }

 

                        trackbackCount += dataService.GetTrackingsFor(potentialEntry.EntryId).Count;

                    }

                }

 

                int commentCount = dataService.GetAllComments().Count;

 

                sb.Append("<div class=\"blogStats\">");

                sb.Append("<table border=\"0\" width=\"100%\">");

                sb.Append("<tr><td align=\"left\">Total Posts</td><td align=\"right\">" + allEntriesCount + "</td></tr>");

                sb.Append("<tr><td align=\"left\">This Year</td><td align=\"right\">" + yearPostCount + "</tr></td>");

                sb.Append("<tr><td align=\"left\">This Month</td><td align=\"right\">" + monthPostCount + "</tr></td>");

                sb.Append("<tr><td align=\"left\">This Week</td><td align=\"right\">" + weekPostCount + "</tr></td>");

                sb.Append("<tr><td align=\"left\">Comments</td><td align=\"right\">" + commentCount + "</tr></td>");

                sb.Append("<tr><td align=\"left\">Trackbacks</td><td align=\"right\">" + trackbackCount + "</tr></td>");

                sb.Append("</table>");

                sb.Append("</div>");

            }

            catch (Exception ex)

            {

                sharedBasePage.LoggingService.AddEvent(new EventDataItem(EventCodes.Error, "KensBlogStats Error: " + ex.ToString(), String.Empty));

            }

            return new LiteralControl(sb.ToString());

        }

 

    }

 

 

}

Posted: Tuesday, October 03, 2006 11:04:24 PM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | C Sharp | Dasblog | Software

So, having recently updated this website to Dasblog 1.9, I decided it was time to check out the source and have a play around.

The initial easy step to get involved seemed to be writing some Macros (I wanted to extend the default blogStats() macro to include Total Trackbacks and also add a Trackback Count to each post item in the footer.

There is a great intro to Creating Dasblog Macros on their Documentation Site, also googling for Dasblog macros uncovered a few good sites

Anyway, I cracked open VS and spent a couple of hours writing some new macros, compiled them, made the requisite changes to my homeTemplate and itemTemplate but they just seemed to be return null strings. Checking the (Dasblog) event log showed that it was failing the find the Type for my Macros.

I checked and rechecked everything and was convinced that everything was as it should be. Stumped!!
Viewed my DLL in reflector, typenames were all correct - what was I doing wrong ??

A quick post to the Dasblog developers mailing list resulted in someone suggesting that I check if I was running a .NET2 compiled DLL in a .Net1.1 IIS virtual directory - Doh !!

Updated the virtual directory to ASP.NET 2.0 (easier than reverting to VS2003 from VS2005), 'touched' the web.config, refreshed the page and 'ta da...'

Changes and source code to be rolled out to this web site in the next day or so (more testing to do on how ASP.NET 2.0 works out),

Posted: Tuesday, October 03, 2006 3:26:44 AM (GMT Daylight Time, UTC+01:00)  #   Comments [0]
TAGS: .NET | C Sharp | Dasblog | Development | Software