Getting levels of the SharePoint Heirarchy and their Exceptions

Something that I have to do time and again is get some element of SharePoint’s heirarchy, such as a site collection, site, list or item. This is pretty typical – that’s why we all use USING to ensure proper disposal of SPSites and SPWebs, right? But what happens if the thing you’re after isn’t there? What exception get’s thrown? Well, this should be pretty clear:

try
{
    //FileNotFoundException if doesn't exist
    using (SPSite site = new SPSite(siteGuid))
    {
        //FileNotFoundException if doesn't exist
        using (SPWeb web = site.OpenWeb(webGuid))
        {
            //SPException if doesn't exist
            SPList list = web.Lists[listGuid];

            //ArgumentException if doesn't exist
            SPListItem item = list.GetItemByUniqueId(itemGuid);
        }
    }
}
catch (System.IO.FileNotFoundException fileEx2)
{
    // Site or Site Collection Not Found
}
catch (SPException spEx2)
{
    // List not found
}
catch (ArgumentException argEx2)
{
    // Item not found
}

Hopefully that might prove useful to someone – and a good reminder for me.

Advertisement
Getting levels of the SharePoint Heirarchy and their Exceptions

Make Visual Studio break on all Exceptions

I’m a big fan of not raising exceptions if possible – rather than throwing and catching ‘expected exceptions’. To me, that phrase is an oxymoron – exceptions should be, um, exceptional, and it can make the wood hard to see for the trees when looking for proper exceptions.

Anyway, that aside – you can make Visual Studio break on any exception, not just the unhandled ones. Use Ctrl-Alt-E to edit – there is supposed to be some way to get to it though the menu, but I’ve not found it. Discovered in a blog post here.

Make Visual Studio break on all Exceptions

C# Code to send an email

I’ve been doing some testing of email enabled lists, and I needed to send quite a lot of emails, so I wrote a little console app to do it. Here’s the core of the code I used, in case I need it again, or it’s useful to someone. It uses System.Net.Mail:

SmtpClient smtp = new SmtpClient(@"vm-moss2007.virtual.local");
for (int i = 1; i <= 100; i++)
{
    MailMessage message = new MailMessage("administrator@virtual.local", <a href="mailto:testlist@sharepoint.virtual.local">testlist@sharepoint.virtual.local</a>);
    message.Subject = string.Format("Message {0}", i);
    message.Body = string.Format("This is message '{0}'", i);
    Console.WriteLine("Sending {0}", i);
    smtp.Send(message);
}
C# Code to send an email

Event Properties AfterProperties – what should they be?

While working on pre-filling ListItem fields on an item, I became a bit puzzled. The SPItemEventProperties.AfterProperties collection is a dictionary which can contain the named value for one of the fields of the item. In other words, if we wanted to set a value “Tax Area” to “Europe” we’d do:

properties.AfterProperties["Tax Area"] = "Europe";

In our case, however, we didn’t know what these properties were before hand. Rather, we were ‘inheriting’ values from a parent folder. Thus, we were going to use the parent folder’s SPField object for each field to define the value. I started out using:

properties.AfterProperties[parentField.Title] = parentItem[parentField.id];

But is Title the right property to use? Well, having looked through a number of blog posts, this seems to be the subject of some confusion.

At first Title is okay to use. However, you can change the display name of the field. For example, we could change our field’s Title to ‘Tax Region’ – but we still need to use ‘Tax Area’ in our AfterProperties collection.

So, InternalName is the right property of the SPField to use – but there is a hiccup. The InternalName is encoded – Tax_x0020_Area – so you have to unescape it like I’ve talked about before.

The summary is, then, use the unescaped InternalName in your AfterProperties collection.

Event Properties AfterProperties – what should they be?

Stupid Visual Studio 2005 MSDN DVD

A note for myself. The MSDN DVD for April 2007 (Disc 3070.1) with Visual Studio 2005 on it can’t be installed directly from the DVD. The installer asks you to ‘Install Disk 1’, which is unfortunate, as there is no disc 1, and the DVD contains both CDs in different directories on it.

The solution is to copy both CDs to a single directory on the hard disc of the machine, and run the installer from there.

Unfortunately, for some reason my virtual machine won’t let me do that directly – it complains about copying one of the files from the DVD – but I can share a folder on the host, and copy the installation files into the VM via that folder.

Once you’ve copied the files on, though, the ‘pre-filled’ licence key isn’t there anymore. You can get the key by starting installing from the DVD, writing it down, and then using it when you install from the hard disc.

It’s crazy the work-arounds you have to go through sometimes 😦

Stupid Visual Studio 2005 MSDN DVD

Visual Studio Comments

I didn’t realise this, but Visual Studio allows you to see comments beginning TODO, HACK and UNDONE in the task list pane. That’s really quite neat, though it’s unfortunate that the list only contains items for files that are currently op open in the IDE. You can also add your own ‘tokens’ via the Options > Task List dialog. Vish has a pretty good summary. It’s just a shame that the task list isn’t for all files, irrespective of whether they’re open or not. Still, a neat feature that I didn’t know about.

Visual Studio Comments

Using the ASP.NET Web Site Administration Tool to test SQL connections

I’ve been setting up forms based authentication for SharePoint. This is pretty much a normal ASP.NET authentication set up, but I’ve been following the instructions from Andrew Connell, and the excellent instructions from Dan Attis.

In these instructions, we set up the ASP.NET database using aspnet_regsql and then use the Web Site Administration tool to check the connections and create users (if you want to know more, check out the articles). However, here I had a bit of a quirky problem. Both sets of instructions say to, in the Web Site Administration tool, select the Provider tab and then select Select a Different Provider For Each Feature (Advanced). Then click Test by each provider. The problem – no Test link beside either provider.

As I was having some problems, I decided to dig into this. I tracked through the code that makes up the Web Site Administration tool, and found that this link is only shown if your provider contains the text ‘Sql’. I kid you not…

Using the ASP.NET Web Site Administration Tool to test SQL connections

I'm now a 'Professional Developer'

MCPD Logo

Presumably, everything I’ve written over the last 7 years has been amateur (well, some of it was, certainly). This is in addition to my being…

MCTS Logo

To be honest, I still feel I’m being firehosed with information about Microsoft stuff, and that I’m struggling to keep up. But I guess that the point of such certifications is that they demonstrate a level of ‘minimum competance’ – that is, the holder still might be “Good” rather than “Brilliant”, but at least you can be fairly sure that they aren’t “Completely Inept”.

Anyway, I learnt a lot doing the Professional Developer exam – or rather, reading the course materials. The exam itself, well, I felt that some of the questions were a bit too technical, while the book seemed to cover a higher level view of the system (at least, in it’s good bits). See my comments on the book I used.

I'm now a 'Professional Developer'