So how to fix this? There are several options available, and one we'll look at here is creating a solution (scoped to the Farm level) that will be used to deploy all of our CSS and scripts for use through-out the farm. Here are some of the benefits to doing it this way:
- You have a single .wsp solution that can be deployed in all environments, making deployments easier.
- We'll be using the Layouts folder, so all code can easily reference this folder with relative paths.
- We'll be using a Feature, so all other solutions can use Feature Dependencies to ensure the CSS and Scripts are deployed.
- This is a fairly easy and straight forward solution and is easy to maintain.
We'll deploy this as a Farm solution:
Now that we have our project, we'll right click on the project name and select Add --> SharePoint "Layouts" Mapped Folder:
This will gives us a Layouts folder in our project and right underneath it another folder with the name of our project "GlobalCssJsFiles". Now we'll right click this folder and add two more folders, "CSS" and "Scripts":
Now, right click the Features folder and choose "Add Feature". This will give you a new Feature and all we need to update is the Title, Description and Scope:
- Title: Global Css and JS Files
- Description: This feature includes all the CSS and JS files needed for the environment.
- Scope: Farm
Now, just as a side note, we did not have to add a feature here. You could package this up and deploy without it, but without the feature you can not do Feature Dependencies in your other projects.
Build your project and deploy it. If you deploy via Powershell you can go into Central Administration under Central Administration --> Manage Farm Features. You will see our feature is here and you can activate it.
Once you activate it, you can go to the server and see the files now in the layouts folder, located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS:
- CSS: src="/_layouts/GlobalCssJsFiles/Css/AwesomeStyles.css"
- Scripts: src="/_layouts/GlobalCssJsFiles/Scripts/AwesomeScripts.css"
Again, this is not the only way to deploy global files, but I find its a nice and simple way to go about it. It makes moving these files between your environments easy and safe, and allows you to use relative paths for all CSS and scripts which is always the best practice.