Random Thoughts of a Scatterbrain.
 Tuesday, May 13, 2008

Impulse Buy...Almost

5/13/2008 7:11:57 PM (Eastern Daylight Time, UTC-04:00)

I usually have problems spending more than a few dollars on anything non-functional.  For example, art or posters (which to my wife's chagrin, leaves most of the walls in our house bare).  However -- for a moment -- I actually considered if I would be willing to go all out and buy a hand written letter from Einstein:

A letter being auctioned in London this week adds more fuel to the long-simmering debate about the Nobel Prize-winning physicist's religious views. In the note, written the year before his death, Einstein dismissed the idea of God as the product of human weakness and the Bible as "pretty childish."

The letter, handwritten in German, is being sold by Bloomsbury Auctions on Thursday and is expected to fetch between $12,000 and $16,000.

I dunno...$16,000 seems like a bargain for a piece of history that seems almost priceless. A hand written letter by one of the greatest scientific minds in human history on one of the most oft debated aspects of his personal life?  $16,000 is a downright steal.

Which reminds me, I should pick up a print of the cover of Newton's Principia Mathematica one of these days.

log4net With SharePoint Layout Page Applications

5/13/2008 12:36:55 PM (Eastern Daylight Time, UTC-04:00)

Using log4net with SharePoint layout page applications is really no different from using it with other types of web applications with the exception that there really isn't a convenient way to initialize the logging configuration from your custom binaries.

The answer lies in the oft overlooked AssemblyInfo.cs file.

Add the following line to the file:

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

You may also want to add log4net binary to the /App_Bin directory of the WSS virtual directory (as well as the configuration into the web.config file of the application, of course).

 Tuesday, May 06, 2008

Buy This Book!

5/6/2008 7:15:31 PM (Eastern Daylight Time, UTC-04:00)

This is quite exciting: my wife is now a published author (with her own ISBN and everything :-D)!

Check out her book The Parent Connection for Singapore Math.

:-D She's also got a media set, you know, if you've got $459 and nothing better to spend it on :-P

SharePoint "Unknown Error" Quirk

5/6/2008 3:16:53 PM (Eastern Daylight Time, UTC-04:00)

Anyone who's done any bit of SharePoint development is probably familiar with the completely useless "Unknown Error" view.  Well, in fact, SharePoint actually knows what the error is, it just doesn't want to tell you (okay, it's really just disabled for users).

In ASP.NET, you can usually get error messages to show up by setting <customErrors="Off"/>

However, this is not sufficient with SharePoint. As Stefan Keir Gordan points out, to get the nitty gritty details, you also need to set <SafeMode CallStack="True""/>

 Monday, May 05, 2008

MbUnit And TestDriven.NET On x64

5/5/2008 12:23:45 PM (Eastern Daylight Time, UTC-04:00)

I came across an interesting issue while trying to run some MbUnit RowTests this morning.

Namely, it seemed that the rows being passed in contained all null values.  It left me scratching my head.  I ran the tests using MbUnit console and it worked fine but didn't work as expected from TestDriven.NET in VS2005.

Well, it turns out that (I think) the install for MbUnit does not create the requisite registry keys in an x64 environment.  It properly creates the keys under the Wow6432Node, but it does not create the keys under the path:

HKEY_LOCAL_MACHINE\SOFTWARE\MutantDesign\TestDriven.NET\TestRunners

So to make it work, all you have to do is to copy the string values from the Wow6432Node to the key above and restart VS.

Hope this saves some headaches for other developers working in an x64 environment!

Update: Jeff Brown notes in the comments that this should be fixed with future releases so that x64 environment registry keys are properly generated.

 Saturday, April 26, 2008

The Sad State of Secularism

4/26/2008 4:26:01 PM (Eastern Daylight Time, UTC-04:00)

This story is kind of upsetting.

Like hundreds of young men joining the Army in recent years, Jeremy Hall professes a desire to serve his country while it fights terrorism.

Known as "the atheist guy," Hall has been called immoral, a devil worshipper and -- just as severe to some soldiers -- gay, none of which, he says, is true. Hall even drove fellow soldiers to church in Iraq and paused while they prayed before meals.

 "I was ashamed to say that I was an atheist," Hall said.

"Religion brings comfort to a lot of people," he said. "Personally, I don't want it or need it. But I'm not going to get down on anybody else for it."

I dunno...it's just kind of unnerving that in this day and age, a person should feel ashamed to be known as an atheist (especially one that is respectful of others' right to believe).  In fact, recent polls have shown that atheists are the most distrusted group in America, even below Muslims:

From a telephone sampling of more than 2,000 households, university researchers found that Americans rate atheists below Muslims, recent immigrants, gays and lesbians and other minority groups in “sharing their vision of American society.” Atheists are also the minority group most Americans are least willing to allow their children to marry.

It just doesn't make sense.

 Thursday, April 24, 2008

Automating Remote GAC Installs On Build

4/24/2008 4:42:15 PM (Eastern Daylight Time, UTC-04:00)

When working with SharePoint, you'll find yourself working with the GAC quite often during development.

If you've seen the light and you're working with SharePoint on a VM, one tricky problem is how to update the GAC using a post-build event.  The following is a little script that I use:

::----------------------- GAC binaries code ------------------------
:: Check if the share is available on the server
IF EXIST "\\server-name\temp" GOTO COPYFILES
GOTO SHOWNOTICE

:: Copy file to the server
:COPYFILES
ECHO Found \\server-name\temp; proceeding to copy files...

SET SN=\\server-name\temp

:: Copy the binary
XCOPY "$(TargetPath)" "%SN%" /Y /I

:: Use PsExec to execute gacutil
PATH=$(SolutionDir)Tools;%windir%\Microsoft.Net\Framework\v2.0.50727;%programfiles%\Microsoft SDKs\Windows\v6.0\Bin;%path%

psexec \\server-name -u Administrator -p password -w "c:\temp" gacutil.exe /i "$(TargetFileName)" /f

GOTO END

:SHOWNOTICE
ECHO Your VM image is not sharing the directory "c:\temp"
GOTO END

:END
ECHO Completed

The batch script makes use of Sysinternal's PsExec.  I've included the binary executable in my solution tree under the directory "Tools".

The script only has one assumption: the VM (or remote machine, really) is sharing the c:\temp directory (okay, and that the path to gacutil.exe has been added to the remote machine's PATH environment variable).  Here's a breakdown of the logic:

  1. The first step is to check if the directory exists and can be reached.  If not, we go to the end and show a notice about sharing the directory.
  2. If the directory is shared, the output dll from the build is copied to the shared directory using plain old XCOPY.
  3. Once it's copied over, we use PsExec to execute gacutil on the VM (or remote machine).  The -w argument specifies the working directory on the remote machine.

Enjoy!

FOR XML Needs More Love

4/24/2008 1:55:27 PM (Eastern Daylight Time, UTC-04:00)

I'm constantly amazed by the number of developers who have never worked with FOR XML EXPLICIT and the new FOR XML PATH.  If I were designing data access, it would be my go-to commands for building queries for complex data structures (nested DataReaders?  yuck!).

In the past, to support paging using FOR XML EXPLICIT queries took tons of lines to accomplish (although there is something about the whole explicitness that makes it surprisingly legible).  Now with the fancy pants ROW_NUMBER function in SQL along with CTEs, a hundred line query can be written with maybe 15-20 lines.

Here's a simple example that you can copy+paste and run:

/* 
Demonstrates usage of ROW_NUMBER and FOR XML PATH to create 
pageable XML results queries.

In this case, the key is to page only on the Route objects.
*/

--// Mock Route table
DECLARE @Route TABLE (
    Id int,
    Title varchar(100)
)

--// Mock Step table
DECLARE @Step TABLE (
    Id int,
    RouteId int,
    Title varchar(100),
    Sequence int
)

--// Insert mock data
INSERT INTO @Route VALUES (1, 'Route 1')
INSERT INTO @Route VALUES (2, 'Route 2')
INSERT INTO @Route VALUES (3, 'Route 3')
INSERT INTO @Route VALUES (4, 'Route 4')
INSERT INTO @Route VALUES (5, 'Route 5')

--// Route 1 Steps
INSERT INTO @Step VALUES (1, 1, 'Step 1.1', 1)
INSERT INTO @Step VALUES (2, 1, 'Step 1.2', 2)
INSERT INTO @Step VALUES (3, 1, 'Step 1.3', 3)
INSERT INTO @Step VALUES (4, 1, 'Step 1.4', 4)
INSERT INTO @Step VALUES (5, 1, 'Step 1.5', 5)

--// Route 2 Steps
INSERT INTO @Step VALUES (6, 2, 'Step 2.1', 1)
INSERT INTO @Step VALUES (7, 2, 'Step 2.2', 2)
INSERT INTO @Step VALUES (8, 2, 'Step 2.3', 3)

--// Route 3 Steps
INSERT INTO @Step VALUES (9, 3, 'Step 3.1', 1)

--// Route 4 Steps
INSERT INTO @Step VALUES (10, 4, 'Step 4.1', 1)

--// Route 5 Steps
INSERT INTO @Step VALUES (11, 5, 'Step 5.1', 1)
INSERT INTO @Step VALUES (12, 5, 'Step 5.2', 2)
INSERT INTO @Step VALUES (13, 5, 'Step 5.3', 3)
INSERT INTO @Step VALUES (14, 5, 'Step 5.4', 4)

/*
Define the page size.
 -- Add sorting and ordering later
*/

DECLARE @PageSize int
DECLARE @CurrentPage int

SET @CurrentPage = 0
SET @PageSize = 3

/*
Calculate starting and ending row.
*/
DECLARE @StartIndex int
DECLARE @EndIndex int

SET @StartIndex = @CurrentPage * @PageSize
SET @EndIndex = @StartIndex + @PageSize

; --// Need to terminate with a semicolon for CTE 
/*
Perform core XML select
*/

WITH Routes AS (
    SELECT 
        *,
        ROW_NUMBER() OVER (ORDER BY Id) AS 'RowNumber'
    FROM
        @Route
)
SELECT
    Routes.Id AS "@Id",
    Routes.Title AS "@Title",
    (
        SELECT
            Step.Id AS '@Id',
            Step.Title AS '@Title'
        FROM
            @Step AS Step
        WHERE
            Step.RouteId = Routes.Id
        ORDER BY 
            Step.Sequence ASC
        FOR XML PATH('Step'), TYPE            
    ) AS 'Steps'
FROM 
    Routes
WHERE
    RowNumber > @StartIndex AND RowNumber <= @EndIndex  --// BETWEEN Results in improper paging
FOR XML PATH('Route'), ROOT('Routes')

What's great about this is that if your object model is properly designed, it's just a matter of deserializing the XML (using precompiled serialization binaries, of course) to rehydrate your data model.

In this case, the output XML looks like this:

<Routes>
  <Route Id="1" Title="Route 1">
    <Steps>
      <Step Id="1" Title="Step 1.1" />
      <Step Id="2" Title="Step 1.2" />
      <Step Id="3" Title="Step 1.3" />
      <Step Id="4" Title="Step 1.4" />
      <Step Id="5" Title="Step 1.5" />
    </Steps>
  </Route>
  <Route Id="2" Title="Route 2">
    <Steps>
      <Step Id="6" Title="Step 2.1" />
      <Step Id="7" Title="Step 2.2" />
      <Step Id="8" Title="Step 2.3" />
    </Steps>
  </Route>
  <Route Id="3" Title="Route 3">
    <Steps>
      <Step Id="9" Title="Step 3.1" />
    </Steps>
  </Route>
</Routes>

Next, you'll need some simple code to deserialize the XML to make it useful:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;

namespace XmlDeserialization {
    internal class Program {
        private static void Main(string[] args) {
            string xml = ...; // XML string here

            RouteList routes = new RouteList(xml);

            Console.Out.WriteLine(routes.Count);

            foreach(Route route in routes) {
                Console.Out.WriteLine(" + Route: {0}", route.Id);

                foreach(Step step in route.Steps) {
                    Console.Out.WriteLine("   + Step: {0}", step.Id);
                }
            }
        }
    }
    
    public abstract class XmlDeserializingList<Titem> : List<Titem> {
        protected XmlDeserializingList() { }

        protected XmlDeserializingList(string xml) {
            StringReader reader = new StringReader(xml);

            XmlSerializer serializer = new XmlSerializer(GetType());
            XmlDeserializingList<Titem> items = (XmlDeserializingList<Titem>)serializer.Deserialize(reader);

            AddRange(items);
        }
    }

    [XmlRoot("Routes")]
    public class RouteList : XmlDeserializingList<Route> {
        public RouteList() {}

        public RouteList(string xml) : base(xml) {
            
        }
    }

    [Serializable]
    public class Route {
        private int id;
        private string title;
        private Collection<Step> steps;

        [XmlAttribute]
        public int Id {
            get { return id; }
            set { id = value; }
        }

        [XmlAttribute]
        public string Title {
            get { return title; }
            set { title = value; }
        }

        [XmlArray("Steps"), XmlArrayItem("Step", typeof(Step))]
        public Collection<Step> Steps {
            get { return steps; }
            set { steps = value; }
        }
    }

    [Serializable]
    public class Step {
        private int id;
        private string title;

        [XmlAttribute]
        public int Id {
            get { return id; }
            set { id = value; }
        }

        [XmlAttribute]
        public string Title {
            get { return title; }
            set { title = value; }
        }
    }
}

It might even be useful to add some abstract methods (and properties to support it) to GetNextPage()

Nuking SharePoint Styles For Layout Page Applications

4/24/2008 8:59:46 AM (Eastern Daylight Time, UTC-04:00)

SharePonit layout apps (as I call them) are the missing link in most developer's understanding of SharePoint and why it seems like it can be a pain in the butt as a development platform.  As I've discussed previously, SharePoint development only deviates from regular ASP.NET development ever so slightly.  A bit of creativity and ingenuity easily overcomes most hurdles.

As I'm doing development in the area, I'd like to help shed some light on the mystery of it all and show how developers and SharePoint can work together in harmony :-)

One of the first steps to making happy with SharePoint when working with layout pages is to nuke the SharePoint CSS which creates weird padding around the main work area.  I'm sure it may have some purpose, but it's irrelevant for us at the moment.

Let's take a look.

In the following screen, I've added a <div/> element with a one pixel, black border with width and height set to 100% (this is in IE7 and it looks about the same in FF2).

Notice the fat white padding on the top and the right.  This is useless and ugly.  Not only that, we didn't acheive the 100% height that we'd like to.  We can nuke it with some CSS:

/*--- override WSS styles ---*/
body { height: 100%; }
.ms-formareaframe { padding: 2px 4px 4px 4px; background: #fff; height:100%; }
.ms-propertysheet { height: 100%; }
#onetidMainBodyPadding { height: 0px; }
#onetidMainBodyPadding img { height: 0px; display: none; }
#onetidYPadding { width: 0px; }
#onetidYPadding img { width: 0px; display: none; }

And here's the result:

Bingo!  It looks kind of the same in FF2 (FF2 overdraws the 100% declaration and cuts off the bottom and right borders; this does require some CSS hackery to fix).

But the big picture is that now we have a blank slate with which to integrate with SharePoint.  You get all of the benefits of SharePoint authentication, document management, profiling, and so on just by putting your ASP.NET application into SharePoint as a layout page.  Development is easier than it would seem since the package, during development, only has to be deployed once.  Otherwise, all of the files can be deployed to SharePoint using an automated XCOPY or manually.  In some cases, for example working with ASPX page layout, the page can be accessed directly via file share (this is how I do all of my layout).

For more info on creating layout apps, check out the following articles on packaging SharePoint solutions:

 Tuesday, April 22, 2008

Quote of the Day

4/22/2008 11:43:04 PM (Eastern Daylight Time, UTC-04:00)

I'm not a metal fan, but I like this one.

"We Need To Let Metal And Odin Catch The Kids Before Jesus Does!" -- HELHEIM

Funny.

RSS 2.0 Atom 1.0 CDF