In SharePoint 2007, upgrading a Web Part to a new version number was a bit complicated. But as Maurice Prather nicely explains in this article, this has become much simpler in SharePoint 2010. Although, due to a small bug, again described by Maurice in another great article, taking advantage of this in existing projects requires a bit of luck. If your public key token contains one or more byte pairs with a leading 0, you are not so lucky. As Maurice explains, the 0 will be stripped from the PublicKeyToken attribute of the BindingRedirect element in web.config. This will of course cause the binding redirect to fail, and the Web Part will render an error message.
I was a bit surprised to see that this was not fixed in SP1, but luckily we didn’t have to wait that much longer. I have just tested this after installing the June CU and now it works as it should! I followed these steps:
- Create an empty SharePoint project.
- Add a strong name key file with a key like this: 0408d0be841e7310.
- Add a web part to the default feature.
- Deploy the project to a web application in a pre-June CU installation of SharePoint 2010.
- Add a web part to a page, and verify that it renders correctly.
- Update the assembly version of the project to 188.8.131.52, add a SafeControl entry for version 184.108.40.206, and a BindingRedirect element to the solution manifest.
- Deploy the project again, and confirm that the web part fails to render. Verify that the strong name key in web.config is incorrect, like this: 48d0be841e7310.
- Update SharePoint to the June CU, and deploy the solution package again. Verify that the web part now works as expected, and that a correct public key token has been added to web.config.