“NuGet is a free, open source developer focused package management system for the .NET platform intent on simplifying the process of incorporating third party libraries into a .NET application during development.”
That description of NuGet comes from the NuGet site over at codeplex. In short it’s a common way of downloading, installing, and managing third party libraries into a .NET application (e.g. getting “rid” of the LIB folder).
Normally when these types of “extensions” or “tools” are delivered to the .NET Visual Studio users they don’t work so well with SharePoint projects since they are primarily built for web/win/WPF/Silverlight applications that have a “normal” packaging system (and we all know that SharePoint has its own WSP thingy way of packaging).
Since my focus is on SharePoint, and thus the SharePoint project system in VS, I at least thought I’d have a go testing it out for a SharePoint project.
First things first
Download and install the NuGet Package Manager from the VS extension manager.
OK, once that’s done let’s go ahead and explore what we can do with NuGet.
JQuery via NuGet
“the primary goal of NuGet is to help foster a vibrant open source community on the .NET platform by providing a means for .NET developers to easily share and make use of open source libraries”, from the words of Phil Haack.
One such open source library that comes to mind is of course JQuery.
Why use NuGet to include JQuery you might wonder? Well, for me it’s simply a question of having someone else (be it a service in this case) managing when JQuery gets updated. I don’t want to keep track of the updates on my third party libraries.
So, let’s see how we can include JQuery via the NuGet Package Manager then.
The first thing you notice after installing the NuGet Package Manager is that you get a new menu option, “add Library Package Reference…”. Let’s click on it and see if we can find JQuery in there somewhere.
After applying a filter for JQuery you see that NuGet finds JQuery and the current release is 1.5.2 (I think 1.6 is on its way and then NuGet will tell me that I hope). Let’s install JQuery and see what happens.
The packages folder contains a subfolder for each installed package (jQuery 1.5.2 in our case). This subfolder contains the files installed by the package. It also contains the package file itself (the .nupkg file, which is a .zip file that contains all of the files included in the package).
Maybe I just got lucky
I didn’t plan on it, I promise, but while writing this post JQuery was indeed updated to version 1.6 so NuGet actually tells me that:
Let’s update JQuery then!
After applying the update my Scripts folder now includes JQuery 1.6
What about SharePoint then
XCopy? …I don’t think so!
I actually tried out a couple of things but the one that seemed to work out best was the following approach.
Mapped folder to the rescue
First of all, start by uninstalling JQuery via the NuGet package manager to remove all the added content (starting out fresh).
Add a mapped folder that points to layouts.
By doing that, depending whether or not you already have a mapped folder for layouts, you get a folder in your VS project.
So NuGet (or should I say the NuGet package for JQuery) expects that a folder named Scripts is found in your project. Well, to keep NuGet happy, renaming the mapped folder to Scripts will not make any affect to the VS packaging system since the deployment location still points to layouts as you can see from the image above. (and yes, you can have multiple mapped folders that are mapped to the same location in root files)
Now, once install JQuery via NuGet and you should see something like this (I added the JQuery folder though):
I prefer to keep my layouts directory a bit tidy so placing the JQuery files in a subfolder called JQuery is the next step (for me at least)
Now you have to move the the JQuery files into the JQuery subfolder and maybe delete some of the files that you don’t want to deploy (maybe the vsdoc file). Also, I prefer not to have the version number included in the file name for JQuery since that is a step that I have to take care of when referencing JQuery in my markup. So, as you can see, I renamed the minified version to simply jquery.min.js
Now, let’s deploy it!
As you can see from the image above, we got the desired effect.
Now NuGet will keep track of when JQuery has an update and you can optionally include it and then you have to repeat some of the steps but, as said, NuGet will keep track out when you have an option to update.
Some notes on using NuGet for third party DLLs
Let’s say we want to include something like Unity in our SharePoint solution. Here are some things to keep in mind when doing that.
Well, the same goes for Unity as for JQuery. Add it through NuGet and it will add references for the DLLs you need. But, since this is SharePoint we have to package the DLL/s we need in our WSP.
The sad story here, are you listening Microsoft, is that there are only two options we have. Add an existing assembly (the LIB folder again) or some output from another project.
I’m really missing the option to include an assembly from my projects references…might have to try to build something like that.
The only option left is to reference the assembly inside the NuGet packages folder. The bad thing about that is that once unity (or what ever NuGet package you have) gets updated I have to re-map that path to the new NuGet folder since the old one is deleted.
If I had the option to just say something like, “include the reference I have in project to Unity as an existing assembly in the WSP” ,NuGet would have taken care of updating my project with the new reference and thus the newer version would have been included in the WSP.
Happy NuGetting folks!!