Thursday, March 6, 2008

Are parents responsible the delinquency of teenaged SPLists?

Ever since I hooked up with Microsoft and began fathering little bits of softwares, I always thought that I would raise good code. However, like many have endured, one of my softwares began acting defiant and outrageous. We debugged the software repeatedly and how stubborn it was! Certainly didn't get any of that from MY influence! ahem! Lets get into the psyche of this little rebel a bit...

public static SPList GetListByName(SPWeb rootWeb, string listName)
{
    foreach (SPList list in rootWeb.Lists)
    {
        if (list.Title.Equals(listName,
            StringComparison.InvariantCultureIgnoreCase))
        {
            return list;
        }
    }
    
    foreach (SPWeb web in rootWeb.Webs)
    {
        SPList resultList = GetListByName(web, listName);
        if (resultList != null)
        {
            return resultList;
        }
    }
    
    return null;
}

Take a look at the base case where the list is found and returned. Pretty standard stuff no? What is not (by even a far stretch of even the most eclectic programmer's imagination) obvious here is that the list being returned has a... how do I say this.... special Views property (of type SPViewCollection). The crime here is that these views are wrong. No, The collection isn't empty, it doesn't throw exceptions when you try to access it, it isn't null, it doesn't contain any number of views other than the amount of views it really does have, and the views aren't uninitialized or anything. They are wrong because they only have two columns, no matter what.

The victim of such a crime is one that wants the list for its views and is given this list as joke. How did I deal with such reckless behavior? Well, it didn't go down without a hairy fight thats for sure, but in the end, I taught it to return regular SPLists that were not special.

SPSite s = SPContext.Current.Site;
SPList l = s.AllWebs[rootWeb.Name].Lists[listName];
return l;

Tuesday, February 5, 2008

Revision# 6,450,002.0 of the command line parameter parsing piece

One of the most re-invented wheels of all time is the one which takes options from a set of command line arguments. It's a fine tradition to reinvent this wheel and here I present the fruits of today's celebration.
static void Main(string[] args)
{
    string option1 = getCommandLineParameter("-option1", args);
    string option2 = getCommandLineParameter("-option2", args);

    if (option1 != null && option2 != null)
    {
        // react to these parameters
    }
    else
    {
        // react to these parameters not being given
    }
}


private static string getCommandLineParameter(string optionName, string[] args)
{
    short i;
    for (i = 0; i < args.Length; i++)
    {
        if (args[i] == optionName)
        {
            if (args[i + 1] != null)
            {
                return args[i + 1];
            }
            else
            {
                return null;
            }
        }
    }

    return null;
}

Monday, January 28, 2008

If operating systems were beers...

  • DOS Beer

  • Requires you to use your own can opener, and requires you to read the directions carefully before opening the can. Originally only came in an 8-oz. can, but now comes in a 16-oz. can. However, the can is divided into 8 compartments of 2 oz. each, which have to be consumed separately. Although soon to be discontinued, a lot of people are going to keep drinking it after it’s no longer available.

  • Mac Beer

  • At first, came only in a 16-oz. can, but now comes in a 32-oz. can. Considered by many to be a “light” beer. All the cans look identical. When you take one from the fridge, it opens itself. The ingredients list is not on the can. If you call the brewery to ask about the ingredients, you are told that “you don’t need to know.” A notice on the side reminds you to drag your empties to the trash can.

  • Windows 3.1 Beer

  • The world’s most popular. Comes in a 16-oz. can that looks a lot like Mac Beer’s. Requires that you already own DOS Beer. Claims that it allows you to drink several DOS Beers simultaneously, but in reality you can only drink a few of them, very slowly. Especially slow if you are drinking the Windows Beer at the same time. Sometimes, for apparently no reason, a can of Windows Beer will explode when you open it.

  • OS/2 Beer

  • Comes in a 32-oz can. Does allow you to drink several DOS Beers simultaneously. Allows you to drink Windows 3.1 Beer simultaneously too, but somewhat slower. Advertises that its cans won’t explode when you open them, even if you shake them up first. You never really see anyone drinking OS/2 Beer, but the manufacturer (International Beer Manufacturing) claims that 9 million six-packs have been sold.

  • Windows 95 Beer

  • No one drinks it much yet, but a lot of people have taste-tested it and claim it’s wonderful. The can looks a lot like Mac Beer’s can, but tastes more like Windows 3.1 Beer. It comes in 32-oz. cans, but when you look inside, the cans only have 16 oz. of beer in them. Most people will probably keep drinking Windows 3.1 Beer until their friends try Windows 95 Beer and say they like it. The ingredients list, when you look at the small print, has some of the same ingredients that come in DOS beer, even though the manufacturer claims that this is an entirely new brew.

  • Windows NT Beer

  • Comes in 32-oz. cans, but you can only buy it by the truckload. This causes most people to have to go out and buy bigger refrigerators. The can looks just like Windows 3.1 Beer’s, but the company promises to change the can to look just like Windows 95 Beer’s - after Windows 95 Beer starts shipping. Touted as an “industrial strength” beer, and suggested only for use in bars.

  • UNIX Beer

  • Comes in several different brands, in cans ranging from 8 oz. to 64 oz. Drinkers of UNIX Beer display fierce brand loyalty, even though they claim that all the different brands taste almost identical. Sometimes the pop-tops break off when you try to open them, so you have to have your own can opener around for those occasions, in which case you either need a complete set of instructions or a friend who has been drinking UNIX Beer for several years.

  • AmigaDOS Beer

  • The company has gone out of business, but their recipe has been picked up by some weird German company, so now this beer will be an import. AmigaDOS Beer never really sold very well because the original manufacturer didn’t understand marketing. Like UNIX Beer, AmigaDOS Beer fans are an extremely loyal and loud group. It originally came in a 16-oz. can, but now comes in 32-oz. cans too. When this can was originally introduced, it appeared flashy and colorful, but the design hasn’t changed much over the years, so it appears dated now. Critics of this beer claim that it is only meant for watching TV anyway.

  • VMS Beer

  • Requires minimal user interaction, except for popping the top and sipping. However, cans have been known on occasion to explode, or contain extremely un-beer-like contents. Best drunk in high pressure development environments. When you call the manufacturer for the list of ingredients, you’re told that it’s proprietary and referred to an unknown listing in the manuals published by the FDA. Rumors have it that this was once listed in the Physicians’ Desk Reference as a tranquilizer, but no one can claim to have actually seen it.

    Note: I did not author this, I found it on the net somewhere.

    An initial public offering

    I no longer know how this thing works and so I must set it free. My old roomate remembers me cracking away at this some years ago - I'll take his word for it. I should probably remove VBScript from my résumé (and good riddance!). Save this text into a text file with a .wsf extension (i.e. playlistgenerator.wsf) It is a playlist generator BTW.
    <job>
      <object id = oRS progid = "ADODB.Recordset"/>
      <reference object = "ADODB.Recordset"/>
      <script language = "VBScript">
        Option Explicit
    
        Sub DefineAndOpenRS()
          'Define and open the disconnected recordset
    
          With oRS
            .ActiveConnection = Nothing
            .CursorLocation = adUseClient
            .CursorType = adOpenStatic
            .LockType = adLockBatchOptimistic
            With .Fields
              .Append "MyStuff", adVarChar, 255
            End With
            .Open
          End With
        End Sub
    
        Sub InsertRow(sData)
          'Add data to a new row in the recordset
    
          oRs.AddNew
          oRS.Fields.Item("MyStuff").Value = sData
        End Sub
    
        Function ConcatRows()
          With oRS
            .MoveFirst
            ConcatRows = ""
            Do
              if oRS.Fields.Item("MyStuff").Value = "playlist generator" or _
                 oRS.Fields.Item("MyStuff").Value = "playlist.m3u" then
                ' do nothing
              else
                ConcatRows = ConcatRows _
                & .Fields.Item("MyStuff").Value _
                & vbNewLine
              end if
    
              .MoveNext
            Loop Until .EOF
          End With
        End Function
    
        Dim lItem, sResults
    
        DefineAndOpenRS
    
        dim fso
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        dim folder
        Set folder = fso.GetFolder(".")
    
        dim fileList
        Set fileList = folder.Files
    
        dim playListFile
        Set playListFile = folder.CreateTextFile("playlist.m3u", true)
    
        dim file
        for each file in fileList
          if file.Name = "playlist generator.wsf" or file.Name = "playlist.m3u" then
            ' do nothing
          else
            InsertRow(file.Name)
          end if
        next
    
        oRS.Sort = "MyStuff ASC"
    
        do while not oRS.eof
          playListFile.WriteLine(oRS.Fields.Item("MyStuff").Value)
          oRS.movenext
        loop
    
        playListFile.Close
    
        sResults = ConcatRows()
        oRS.Close
        MsgBox sResults, vbOkOnly
      </script>
    </job>