Gotcha When Using the SharePoint Link to a Document Content Type


The “Link to a Document” content type in SharePoint allows you to add URL links to a Document Library, much like you would with a Links List, but in this case you can add a Link item to a document library without having to add a document.

The “Link to a Document” content type is very simple containing only a single column of type Hyperlink or Picture;

doclink-00a

Once added to a document library, you can create a new link using the New Document menu from the ribbon, you are navigated to a layouts page called NewLink.aspx;

doclink-00c

On here you enter the ‘Document’ link name (this is the links name or label you’ll see in list views etc) and the actual URL itself.

Click OK and your link is added to the library, notice the items ‘Link’ icon overlay.

doclink-00d

Clicking on the ‘document’ then navigates you to the URL you entered previously.

What the ‘NewLink.aspx’ page does is create an ASPX page containing a URLRedirector control, this control then picks up the value of the ‘URL‘ column off the current item and redirects to the URL column value – pretty cool right.

<%@ Assembly Name='Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' %>
<%@ Register TagPrefix='SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %>
<%@ Import Namespace='System.IO' %>
<%@ Import Namespace='Microsoft.SharePoint' %>
<%@ Import Namespace='Microsoft.SharePoint.Utilities' %>
<%@ Import Namespace='Microsoft.SharePoint.WebControls' %>
<html xmlns:mso="urn:schemas-microsoft-com:office:office" xmlns:msdt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882">
<head>
<meta name='progid' content='SharePoint.Link' />
<!--[if gte mso 9]><SharePoint:CTFieldRefs runat=server Prefix="mso:" FieldList="FileLeafRef,URL"><xml>
<mso:CustomDocumentProperties>
<mso:ContentTypeId msdt:dt="string">0x01010A00BD153E6070CBF84FAA8B5C218F4038F8</mso:ContentTypeId>
<mso:IconOverlay msdt:dt="string">|aspx?ID=480&amp;FolderCTID=0x0120D520004E6A9CE40535BC45BDD855B8E4BF918C0100913C71E2EADBD74C872C2EB4FA252649&amp;List=c54f061e-1de8-4c30-a4fb-bc35d7d76658&amp;RootFolder=%2Fsites%2Fcat%2FS|linkoverlay.gif</mso:IconOverlay>
<mso:URL msdt:dt="string">http://pub.pdogs.local/SGM Meetings/Forms/SGM Documents Set/docsethomepage.aspx?ID=480&amp;FolderCTID=0x0120D520004E6A9CE40535BC45BDD855B8E4BF918C0100913C71E2EADBD74C872C2EB4FA252649&amp;List=c54f061e-1de8-4c30-a4fb-bc35d7d76658&amp;RootFolder=%2Fsites%2Fcat%2FS, http://pub.pdogs.local/SGM Meetings/Forms/SGM Documents Set/docsethomepage.aspx?ID=480&amp;FolderCTID=0x0120D520004E6A9CE40535BC45BDD855B8E4BF918C0100913C71E2EADBD74C872C2EB4FA252649&amp;List=c54f061e-1de8-4c30-a4fb-bc35d7d76658&amp;RootFolder=%2Fsites%2Fcat%2FS</mso:URL>
</mso:CustomDocumentProperties>
</xml></SharePoint:CTFieldRefs><![endif]-->
</head>
<body>
<form id='Form1' runat='server'>
<SharePoint:UrlRedirector id='Redirector1' runat='server' />
</form>
</body>
</html>

Gotcha

When you try to add a long URL to a document link, you’ll see a validation error message;

doclink-01

This is a bit weird AFAIC, the ‘Document URL’ input field is restricted to 255 characters, which is correct given that a Hyperlink field is being used as backing storage, and the ‘Document Name’ field shouldn’t have any bearing on the length of the URL (255 character limit aside).

The culprit is the validation function used on the page, shown below;

function ValidateInput()
{ULSvlv:;
var form = document.forms.aspnetForm;
var folderUrl = form.ctl00_PlaceHolderMain_ctl01_ctl01_UrlInput.value;
var name = form.ctl00_PlaceHolderMain_ctl01_ctl01_NameInput.value;
if (name == null ||
folderUrl == null ||
typeof(name) == "undefined" ||
typeof(folderUrl) == "undefined" ||
name.length == 0 ||
!HasValidUrlPrefix(folderUrl))
{
alert(L_EnterValidUrl_Text);
return false;
}
if (name.length > 128 ||
name.length + folderUrl.length > 260)
{
alert(L_ItemOrFolderNameTooLong);
return false;
}
return true;
}

For no good reason I can think of, its adding the length of the ‘Document Name’ field to the length of the ‘Document URL’ field, and if its more than 260 characters you get the error message. But these 2 elements are not related in this context and so the comparison is incorrect;

  1. The ‘Document Name’ value is stored in the Title/FileLeafRef column
  2. The ‘Document URL’ value is stored in the ‘URL’ columns

Personally, I think this code is more relevant when creating Folders, where the folder name has a direct bearing on the items final SharePoint URL.

Solution

You could modify the NewLink.aspx file in the 15 hive.

I’m being rhetorical of course, you wouldn’t really want to do that.

The other option you have is to load some javascript into your masterpage to overwrite the OOTB validation function, you’d just need to make sure it appears low down on the page and that it does nothing if the ‘NewLink.aspx’ page isn’t loaded;

window.ValidateInput = function()
{ULSvlv:;
var L_ItemOrFolderNameTooLong_Ex = "The specified document name or URL is too long. The URL must be less than 255 characters and no more than 128 characters for the document name.\nPlease enter a shorter document name or URL.";
var L_EnterValidUrl_Text_Ex = "Enter a valid document name and URL. Valid URLs must begin with \u0027http:\u0027 or \u0027https:\u0027 and must be less than 255 characters.";
var form = document.forms.aspnetForm;
var folderUrl = form.ctl00_PlaceHolderMain_ctl01_ctl01_UrlInput.value;
var name = form.ctl00_PlaceHolderMain_ctl01_ctl01_NameInput.value;
if (name == null ||
folderUrl == null ||
typeof(name) == "undefined" ||
typeof(folderUrl) == "undefined" ||
name.length == 0 ||
!HasValidUrlPrefix(folderUrl))
{
alert(L_EnterValidUrl_Text_Ex);
return false;
}
if ((name.length > 128) ||
(folderUrl.length > 255))
{
alert(L_ItemOrFolderNameTooLong_Ex);
return false;
}
return true;
}

In the code above, I check that the ‘Document Name’ field value is less than 128 characters and the ‘Document URL’ field value is less than 255 character, of course before running that code you should check that the NewLink.aspx page is loaded.

A final version may look something like this;

(function (window) {
// are we on the NewLink.aspx page
if (!window.location.href.match(/_layouts\/NewLink.aspx\?/gi)) return;
/* is it for adding an item of a content type
inheriting the 'Document Link' content type item? */
var params=window.location.search.split('?')[1].split('&');
var s=$rb.grep(params, function(e) {
var p = e.split('=');
return (p.length > 1) && p[0].match(/ContentTypeId/gi) && p[1].match(/^0x01010A/gi);
});
if (!s || !s.length) return;
/* now rewrite SharePoint's ValidateInput(...) function */
window.ValidateInput = function()
{ULSvlv:;
var L_ItemOrFolderNameTooLong_Ex = "The specified document name or URL is too long. The URL must be less than 255 characters and the document name must be less than 128 characters.\nPlease enter a shorter document name or URL.";
var L_EnterValidUrl_Text_Ex = "Enter a valid document name and URL. Valid URLs must begin with \u0027http:\u0027 or \u0027https:\u0027 and must be less than 255 characters.";
var form = document.forms.aspnetForm;
var folderUrl = form.ctl00_PlaceHolderMain_ctl01_ctl01_UrlInput.value;
var name = form.ctl00_PlaceHolderMain_ctl01_ctl01_NameInput.value;
if (name == null ||
folderUrl == null ||
typeof(name) == "undefined" ||
typeof(folderUrl) == "undefined" ||
name.length == 0 ||
!HasValidUrlPrefix(folderUrl))
{
alert(L_EnterValidUrl_Text_Ex);
return false;
}
if ((name.length > 128) ||
(folderUrl.length > 255))
{
alert(L_ItemOrFolderNameTooLong_Ex);
return false;
}
return true;
}
})(window);

Published by

Phil Harding

SharePoint Consultant, Developer, Father, Husband and Climber.

One thought on “Gotcha When Using the SharePoint Link to a Document Content Type

  1. Another ‘gotcha’ is when you find those links in search results; Sharepoint will download the shortcut file. I’ve tried uploading a redirect .aspx page instead, but this seems to have been disabled, forces a download now. Any ideas?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.