Thursday, October 11, 2012

How to make pretty URLs for files in Layouts folder

I know what you would feel after reading this post. It seems so easy. And actually it should be the first thing which comes to developer's mind. But nowadays we're so spoiled with different CMS that we forget about basic things.

So, the task is to make URLs prettier for files located in 14\Layouts folder.
Let's say we have a page which references /_layouts/your.project.name/scripts/thursday.js. Sometimes it might not look very good. Moreover if it's public site than you might not want to scream: "This is Sharepoint!!!!!".

Let's say you want URL to be something like /style/scripts/friday.js. How would you do that? You think about some kind of URL rewriting, aren't you? Maybe about writing some custom http module/handler? Anything else?

The solution is so obvious and easy, so shame to you pal.
What you need to do is:

  • Open IIS Manager
  • Right click on your site
  • Add Virtual Directory
  • Set alias, e.g. style
  • Set path to your folder in 14\Layouts hive



Then just type:
http://coolsite/style/scripts/yourcoolscript.js and go take a couple of beers. That's just it.

And don't forget: let's not make things complicated. We have our lovely women for that ;)

Where to deploy frontend files? Style Library vs _Layouts folder

I'm not going to repeat the stuff you already familiar with. The goal of this post is to summarize some findings and make a personal conclusion based on experience without requirement to read a lot.

Where to deploy css and JavaScript files? Basically we have 2 options:

  • SharePoint layouts folder
  • "Style Library" document library

Style Library benefits:

  • Resources are limited to site collection
  • Smart caching (if it was deployed)
  • Ability to update it on couple of front-end servers (after that you can't deploy new version of file without resetting to site definition) through UI
  • Style Library in URL looks prettier than _LAYOTUS (at first look)

Style Library Problems:
  • Frontend guys are not really good in merging of .spdata files
  • Frontend guys are not really good in modifying path attributes in Elements.xml
  • Actually you need to be prepared to spend 10-50% of your time helping to Frontend guys
  • Deployments during development phase are painful
  • If file was updated manually on production you will forget to reset it
  • Lower performance
  • It's really annoying
  • Caching is not as smart as you might think. Especially with custom NLB.
Layouts folder benefits:
  • Easy to use, straightforward
  • No iisreset/application pool recycling required. You can modify files directly in 14 hive, then Ctrl+F5 and voila!
  • Deployment issues? Forget about them.
  • Merging issues? Forget it
  • Better performance
  • You can be sure that nobody modified your files manually. Sometimes it happens.

Layouts folder Problems:
  • Not scoped. You need just to organize your files
  • Caching. More on that later (few days hopefully)
  • Ugly URLs. More on that
  • Sandbox solutions. Probably it won't work there. But I haven't really worked with them because I don't see any value in Sandbox solutions at this point.
Conclusion: use Layouts folder for your frontend files and be happy with SharePoint. Don't make things more complicated than they are.

Interested links:
http://sharepoint.stackexchange.com/questions/2219/css-files-in-layouts-or-document-library?lq=1
http://www.sharepointnutsandbolts.com/2011/11/avoiding-bugs-from-cached-javascript.html
http://www.sharepointnutsandbolts.com/2009/05/optimization-blob-caching-and-http-304s.html