General
When creating sites through the SharePoint Object Model, the Web Services or the Web Interface you need to filter the illegal characters when specifying the Site Name (which is the part of the URL that references your site).
Here's a list of common illegal characters: # % & * { } \ : < > ? / +
In addition to this list it is prohibited to have consecutive periods in the URL. It's also prohibited to start or end with a space or underline, or to end with a period.
I've found out by experience that non-consecutive periods in Site Names are allowed, but then MOSS 2007 Search doesn't show these sites in its results, so it's best to have no period at all in your Site Name.
Finally there's the comma. It's not on the illegal character list, but be cautious when allowing it. If you add a link to that site through the SharePoint Object Model you might run into problems because the URL Field stores both URL and Description separated by ', ' (a comma followed by a space). The code will think your comma is part of the separator. This in turn can be solved using an UrlEncode but this is easily overlooked.
Code Sample
// Use SPUrlUtility for removal of illegal characters: # % & * { } \ : < > ? /
int illegalCharIndex = SPUrlUtility.IndexOfIllegalCharInUrlLeafName(newSiteName);
while (illegalCharIndex > -1)
{
newSiteName = newSiteName.Remove(illegalCharIndex, 1);
illegalCharIndex = SPUrlUtility.IndexOfIllegalCharInUrlLeafName(newSiteName);
}
// Extra removal of illegal characters ignored by SPUrlUtility: + . ,
newSiteName = newSiteName.Replace("+", "");
newSiteName = newSiteName.Replace(".", "");
newSiteName = newSiteName.Replace(",", "");
// Removal of leading/trailing spaces and underscores
newSiteName = newSiteName.Trim(' ', '_');
You could of course avoid SPUrlUtility and manually strip all illegal characters, which might be less trouble alltogether.
Applies To
WSS 2.0, WSS 3.0