Home » DVWP » How to get the %20 out (or really how to never have it in the first place)

How to get the %20 out (or really how to never have it in the first place)

I’m on a mission. A mission to get the %20 out of SharePoint URLs. They’ve been around long enough, I think; years, now. So ugly. So problematic. Let’s just get rid of them, OK?

You see them everywhere, including the URL for the default “Shared Documents” library that comes with a SharePoint team site. What are they and why are they there? How can we make them go away?

What are They?

The specification for URLs limits the use of allowed characters in URLs to only a subset of the US-ASCII character set. This subset of allowed characters does not include the space character. When you want to use a non-allowed character in a URL, you have to encode it using the “%” symbol followed by the two-digit hexadecimal representation of the ISO-Latin code for the character. (OK, I’m going to stop with the jargon now.)

Straight up, %20 is the URL encoding for a space, and when you create anything in SharePoint that doesn’t specifically ask you for an URL, like a list or library, you will see that %20 show up every single time if you create one with a multi-word name. For URLs this is a little inconvenient, unsightly and sometimes problematic, but for column names, this can be a programming nightmare. If you would allow me to wander onto another related subject for a moment, I’ll explain.

I Digress, just slightly, but for a good reason

A site column (or a list column) has two types of IDs; the first is a unique name and the second is a unique ID. The unique ID is a GUID and you can always get to everything with that if you can figure out what it is. The unique name has two parts though, the internal name and the display name, both of which are stored in the SharePoint database as metadata about the column when you create it. The internal name is stored so that machine languages, such as XML, can read it, and since we’re talking machine and not human language here…you guessed it…any special characters in the name are…escaped! Special characters are replaced with their hex value prefaced and followed by underscores, so space characters look like _x0020_. SharePoint uses the internal name to access the column in the browser, so the URL to access the column now contains %5Fx0020%5F because the underscore (a special character) has to be URL encoded.

This is a little easier to understand using examples, so follow me here:

A column name created with a display name as innocuous as, say,

Nasty hex # - / %

has an internal name of

Nasty_x0020_hex_x0020__x0023__x0

which becomes

Nasty%5Fx0020%5Fhex%5Fx0020%5F%5Fx0023%5F%5Fx0

in the URL.

If this were a list or library name instead of a column name, and used in the URL to your site, just imagine what that looks like emailed out to the group as a link. Yuk. Depending on what you might be doing in SharePoint Designer, like DVWPs, you may get lucky and get to program with the only slightly less cumbersome

Nasty_x0020_hex_x0020__x0023__x0

Hey! you say, I’ve noticed that the whole name is not represented here. Ah, I say, that’s because it gets truncated to 32 characters when it’s stored in the database. Special, right? So, be warned, don’t do what I did when I first started out and create column names that all begin with the project name followed by the purpose of the column. For example, DistributedCodeReviewProjectManager and DistributedCodeReviewProjectManagerComments. If you do, SharePoint will truncate the name and add incremental characters to the end so that it can distinguish between the two, but in SharePoint Designer, YOU are going to have a hard time figuring out which column is which, trust me. Save those precious characters for something meaningful.

So, how do we make them go away?

Hopefully, if you’ve borne with me through this long-winded post, I’ve convinced you of the evil of all special characters and not just spaces. Your reward is that I’m going to tell you how to get them out of your URLs and your column names. (Come in closer, this is a big secret.)

Once you create a column, the internal name can never be changed. It can’t. So when you first create a list, library or column, heck even a view name, create the name without any spaces or special characters. For instance:

Manager Report # should be created as ManagerReportNumber
% Complete should be created as PercentComplete
Roles/Responsibilities should be created as RolesResponsibilities

(The capitalization is not important but only makes column names more readable when the letters are all scrunched together. Readable is better, because when an InfoPath browser-based form displays error messages, it uses the internal name and not the display name. Think of how many fewer support calls you could get if you didn’t need to be the translator to those error messages!)

Now obviously, in your list or library, you don’t want to have column names showing without any spaces and special characters do have their proper usages. So after you have created the column, list, library, site, etc. without spaces or special characters, you can then go back and rename it anything you want it to be by going to the settings for the list, library, site or column. Just don’t make your display name too far off of your internal name; or you’ll still be getting the calls to translate those browser-based InfoPath error messages!

CMNT_CLSD