Tip: Locating the PublicKeyToken of your assembly

Today I ran into a problem concerning a custom webpart. The webpart compiled, so I shipped it ;). But the designtime/compile success was not a guarantee for runtime success. I ran into the following error: Could not load file or assembly ‘xxx, Version=, Culture=neutral, PublicKeyToken=xxx’ or one of its dependencies. The system cannot find the file specified.

So what does this mean? For me the problem was that my libraries were re-signed and in my code there was something like this: <%@ register=”” tagprefix=”CustomControls” namespace=”xxx” assembly=”SharePointControls, Version=, Culture=neutral, PublicKeyToken=123456789″> While in the GAC the assembly had another PublicKeyToken.

When resigning your assemblies, the public key tokens change and an assembly (dll) with the same name is seen as a different version. Which is quite logical because that is what signing with a strong name is meant for.

Next problem is how to get the strong name of my newly signed Assembly, so we can change it in our code. A solution is to deploy your assembly in the GAC and look the PublicKeyToken up in the Assembly folder. There is another solution which is more permament colleague of mine showed it to me:

In Visual Studio, click “Tools” -> “External Tools”. This will open a window in which you can add the command SN as a tool. In the window click “Add” and fill in the fields as seen in the image below. Don’t forget to check the “Use Output window” check box.

PublicKeyToken Tool

When done you can use the tool by selecting the assembly of which you want the PublicKeyToken and clicking the tool  in Visual Studios “Tools” menu. The PublicKeyToken will be displayed in the output window.

That’s all for now folks, happy SharePointing.