Thursday, May 8, 2014

Deploying 3rd-Party Assemblies With Your SharePoint Project


Why would I need to do this?

You have a 3rd-party assembly (DLL) you want to take advantage of in your SharePoint project. You need to make sure that when your WSP is deployed, this 3rd-party assembly goes along for the ride. I'll demonstrate how I accomplish this, by using my SPMeta-Northwind project as an example.


Solution Folders

I got into the habit years ago of placing 3rd-party assemblies in a solution folder. This is a fine way of ensuring that the assemblies are included with your solution when a member of your team "gets latest" from your source control system of choice (TFS, Git, etc.). If you have multiple projects in your solution that need the 3rd-party assembly, they can all reference the one location in the solution folder.

The initial setup of solution folders can be a little tricky, so I'll show step by step how to do it (in Visual Studio 2012). The trick is to create a directory structure on your file system, and then mirror it with solution folders in Visual Studio.

In Windows Explorer, browse to the directory where the solution exists. We're going to manually create a directory called SolutionItems.


Drill into that directory and create another directory called Assemblies.


Since SPMeta-Northwind relies on Camlex.NET, I'm going to place the Camlex.NET.dll assembly here.


Right-click on the solution, and choose Add -> New Solution Folder.


Name the solution folder SolutionItems.


Right-click on SolutionItems and again choose Add -> New Solution Folder.


Name this solution folder Assemblies.


Right-click on Assemblies and choose Add -> Existing Item.


Browse to the Assemblies directory where we placed the Camlex.NET.dll assembly earlier, and add the file.



Package.package

The next step is to update the Package.package file in each of your projects that depend on the 3rd-party assembly. Since SPMeta-Northwind, at present, only has one project, we just have one package to update.

Expand the Package folder, and double-click on Package.package.

Switch to the Advanced tab, and click on the Add button to the right, and choose Add Existing Assembly.

In the Add Existing Assembly dialog, for the Source Path, browse to the location of the Camlex.NET.dll file. Click OK to close the dialog.


Now the Camlex.NET assembly is included in our package and will be deployed to the GAC when our WSP is deployed.



References

The last step is to add a reference to the assembly in each project that needs it.

Under the Northwind.Common project, right-click on References and choose Add Reference. In the dialog, click Browse..., locate the Camlex.NET.dll file in our Assemblies folder, and click Add.


Click OK to close the dialog.

Now the project has a reference to the assembly, and you're ready to go.



Closing

I've found in practice that this is a great way to manage 3rd-party assemblies. You ensure that every developer on your team will get the same file in the same place when they get latest from source control. You ensure that all the projects that reference the assembly have resilient relative paths that are guaranteed not to break across machines. And you ensure that when you build a WSP from your project, it will contain the assembly, and the assembly will be deployed to the GAC when the WSP is deployed.

3 comments:

  1. Hi there,

    I know this is a bit old post but I have issue for adding 3rd party dlls.
    Following all the instructions when I try to deploy the project from Visual Studio I get the error that says : "This solution contains one or more assemblies targeted for the global assembly cache. You should use a strong name for any assembly that will be in the global assembly cache."

    ReplyDelete
    Replies
    1. @rezasharp:

      Assemblies must have a strong name before they can go in the GAC. Most third-party assemblies will already be setup with a strong name. If not, you can try this to give them a strong name yourself:

      https://blogs.msdn.microsoft.com/miah/2008/02/28/how-to-assign-a-strong-name-to-an-unsigned-3rd-party-assembly/

      Delete