Tuesday, 17 September 2013

SharePoint: Visual Web Part CSS Reference

Adding a CSS reference to a visual web part is quite simple:

<SharePoint:CssLink ID="Test" runat="server" DefaultUrl="Url/blah.css" />

The problem comes when referencing a relative url with spaces in it. To URL Encode or not, that is the question.

Initially, I have the reference as DefaultUrl="/Style%20Library/blah.css", but it was not being picked up by the web part. When I opened the source of the page, I found why - the url was being URL encoded by Sharepoint. This means that the URL in the source was

<link rel="stylesheet" type="text/css" href="/Style%2520Library/blah.css"/>

The URL was now double encoded.

I removed the %20 from the Url to resolve the problem.

No comments:

Post a comment