CHunky Universe of Vigourous Astonishing SHarepoint :)

Tag Archives: @Page

javascript: Remove illegal characters in url

Illegal characters

Recently I needed to create valid urls for pages using javascript. Also this time I thought it must be some out-of-the-box code for that in SharePoint. The first thing I came up  was the “Add page” dialog.  I found that the actual dialog was in the _layouts virtual folder:


Bingo, it does the validation in the client side. This is the responsible javascript function that resides directly in the page:

function UpdateUrl() {
  var hiddenPageUrlLabelExtensionClientId = "<%=hiddenPageUrlLabelExtension.ClientID%>";
  var hiddenPageUrlLabelExtension = document.getElementById(hiddenPageUrlLabelExtensionClientId);
  var pageNamePreviewUrlLabelClientId = "<%=pageNamePreviewUrlLabel.ClientID%>";
  var pageNamePreviewUrlLabel = document.getElementById(pageNamePreviewUrlLabelClientId);
  if( pageNamePreviewUrlLabel != null ) {
    var nameInputTextBoxClientId = "<%=nameInput.ClientID%>";
    var nameInputTextBox = document.getElementById(nameInputTextBoxClientId);
    var allowSpaces = false;
    if( GetInnerText(hiddenPageUrlLabelExtension) != "" ) {
      var suggestUrlValue = "";
      for (var i=0; i < nameInputTextBox.value.length; i++) {
         var currentChar = nameInputTextBox.value.charAt(i);
         if (IndexOfIllegalCharInUrlLeafName(currentChar) == -1 
			&& !(currentChar == ' ' && allowSpaces == false) 
			&& currentChar != '.' && currentChar != '+') {
           suggestUrlValue += currentChar;
         else if (currentChar == ' ' 
			|| currentChar == '+' 
			|| (currentChar == '.' && i > 0 && i < (nameInputTextBox.value.length - 1))) {
          suggestUrlValue += '-';
      UpdatePreviewUrl( suggestUrlValue );
    } else {
      if( g_timerId != 0 ) {
      g_timerId = window.setTimeout(OnFriendlyUrlNameChanged, 500);

This function iterates through all the characters in the page url and removes the illegal characters. The space, plus sign and the dot become a hyphen. To determine if a character is illegal, it relies on another javascript function called: IndexOfIllegalCharInUrlLeafName. This function can be found in the init.js or init.debug.js:

function IndexOfIllegalCharInUrlLeafName(strLeafName) {
    for (var i = 0; i < strLeafName.length; i++) {
        var ch = strLeafName.charCodeAt(i);

        if (strLeafName.charAt(i) == '.' && (i == 0 || i == strLeafName.length - 1))
            return i;
        if (ch < 160 && (strLeafName.charAt(i) == '/' || !LegalUrlChars[ch]))
            return i;
    return -1;

This function checks a char against an array of all characters: LegalUrlChars from the same file: init.js.


To use this UpdateUrl function, we have to remove the references to the fields from the Add Page Dialog. Of course, we won’t overwrite this original function, we don’t want to break the SharePoint OOB functionality. Here is how my new function looks like:

var takana = {};
function takana.updateUrl(url) {
  var allowSpaces = false;
  if( url )
    var suggestUrlValue = "";
    var length = url.length;
    for (var i=0; i < length; i++)     {
       var currentChar = url.charAt(i);
       if (IndexOfIllegalCharInUrlLeafName(currentChar) == -1
            && !(currentChar == ' ' && allowSpaces == false)
            && currentChar != '.' && currentChar != '+') {
         suggestUrlValue += currentChar;
       else if (currentChar == ' '
            || currentChar == '+'
            || (currentChar == '.'
                     && i > 0
                   && i < (nameInputTextBox.value.length - 1))) {
         suggestUrlValue += '-';

Server Side

For those of you who want run this operation on the server, here is the code written in C#:

//" # % & * : < > ? \ / { } ~ |
var illegalChars = @"[""#%&\*:\<\>\?\\\/\{\}~\|]";

pageName = Regex.Replace(pageName, illegalChars, string.Empty);

var punctuation = @"[\s\.;\+]";
pageName = Regex.Replace(pageName, punctuation, "-");

//remove "--"
pageName = Regex.Replace(pageName, @"\-{2,}", "-");

pageName = string.Format("{0}.aspx", pageName);

//do it like the built-in dialog, lower case
pageName = pageName.ToLower();

Don’t forget to leave a comment if you find this post useful.

Provisioning multiple pages with one source file

I found an easy way to provision multiple pages. Create a module. Add an aspx.file (e.g. default.aspx). In the elements file define nodes for every page. Path should be the same for all pages, but the Url should be your destination page.

The “default.aspx” (name doesn’t matter) can contain content, or just one @Page directive if Publishing feature is available:

<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

In the Element.xml define all your pages:

In every File node you can add webparts, here is an example:

<File Path="ABunchOfPages\default.aspx"
  <Property Name="Title" Value="About cars" />
  <Property Name="PublishingPageLayout"
		Value="~SiteCollection/_catalogs/masterpage/MyLayout.aspx, Text page" />
  <Property Name="ContentType"
		Value="$Resources:cmscore,contenttype_welcomepage_name;" />
  <AllUsersWebPart WebPartZoneID="Top" WebPartOrder="2">
	   <WebPart xmlns="">
		  <Assembly>Microsoft.SharePoint, Version=,
				Culture=neutral, PublicKeyToken=71e9bce111e9429c
		  <Title>About cars</Title>
				Lorem ipsum dolor sit amet

Of course we could do it in the onet.xml file, but in this case I wanted to show how to do it with an additional module. It is a good way to control the content which is provisioned. And your onet.xml is not so big.

The last thing is to add a web scoped feature which will have this module and add the new feature reference into a site configuration (SiteFeatures section) in the onet.xml.

In this example we provision pages to Pages library ($Resources:cmscore,List_Pages_UrlName;):

<Module Name="ABunchOfPages"

Inside SharepointIt works fine to use our default.aspx with redirection directive (TemplateRedirectionPage):

<%@ Page Inherits="Microsoft.SharePoint.Publishing.TemplateRedirectionPage,
   Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

But what if we want to save these pages in a different location? Perhaps use the same url as the module name (ABunchOfPages). Inside Sharepoint describes how to do this. When you deploy the module with the custom url (ABunchOfPages) a new folder in the web root folder will be created and the new pages will be created in this new folder.

The problem is, you won’t be able to use the redirection ability and if you navigate to one of your new pages an unexpected error will be shown.

In the log file there are more details:

System.NullReferenceException: Object reference not set to an instance of an object.
	at Microsoft.SharePoint.Publishing.TemplateRedirectionPage.ComputeRedirectionVirtualPath(TemplateRedirectionPage basePage)
	at Microsoft.SharePoint.Publishing.TemplateRedirectionPage.get_RedirectionUrl()
	at Microsoft.SharePoint.Publishing.TemplateRedirectionPage.ProcessRequest(HttpContext context)
	at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
	at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)	92f74450-a50b-416d-9b03-25ef15e3c845

So you have to use another aspx-file to provision your pages. Or you can create another publishing document-library in the same web, although it is not recommended according Microsoft.

~masterurl/default.master & ~masterurl/custom.master

Läser “SharePoint 2010 as a Development Platform”. Kan verkligen rekommendera den. Idag har jag förståt vad default.master och custom.master innebär. De pekar på de master-filer som är inställda på web-nivå. Så det är ingen idé att ändra DynamicMasterUrl i @Page-direktivet till sin egen (om du inte vill ha någon helt annan master än i resten av portalen).

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager - Architect, Speaker, Author

Microsoft 365, SharePoint and Azure

SharePoint Dragons

Nikander & Margriet on SharePoint

Cameron Dwyer

Office 365, SharePoint, Azure, OnePlace Solutions & Life's Other Little Wonders


Me and My doings!

Share SharePoint Points !

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

Aryan Nava

DevOps, Cloud and Blockchain Consultant


SharePoint for everyone


Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint


Mostly what I know and share about...


SharePoint på ren svenska