Saturday 23 June 2012

Microsoft.Sharepoint.Sandbox.dll

Just a quick post about an error I recently came across, namely, this one:

"Could not load file or assembly 'Microsoft.Sharepoint.Sandbox, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. An attempt was made to load a program with an incorrect format."

So, how did I come across this?

I wanted to do some development on a SharePoint 2010 project but, I didn't want to do it on a server that had SharePoint installed (the code path used on a non-SharePoint server would mean that the SharePoint features wouldn't have been used).

To achieve this, I needed to copied across the Microsoft.Sharepoint.dll over to the non-SharePoint server and then have the project reference this DLL so that everything compiles correctly. I should add here that the server I copied the DLL from was a 64-bit server and the non-SharePoint server is a 32-bit server.

Upon compilation, everything is fine and dandy, everything compiles and there's no problems. However, at runtime, I get the above error. But why?

Well, there's two important parts to this error message...

Could not load file or assembly 'Microsoft.Sharepoint.Sandbox, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies.

This tells us what file is causing the problem. Microsoft.SharePoint.Sandbox? We didn't include that! As it turns out, when you reference the Microsoft.Sharepoint.dll, upon building the project, this is automatically put in to your web applications bin directory.

The second part tells us why it's causing us a problem...

An attempt was made to load a program with an incorrect format.

This occurs when you try to run a 64-bit DLL on a 32-bit process. 

So, we now know what's causing the problem and why. How do we solve this problem? Simple. We delete it! On a non-Sharepoint server, the Microsoft.Sharepoint.Sandbox.dll isn't used so it can be removed from the bin directory of your web application. The best way to do this is to add a post-build event to your project which deletes the DLL so you don't have to do anything. Obviously however, if this is used on a SharePoint server you will need that DLL so just be aware of that.

And that's it, another odd problem solved.

1 comment:

  1. It helped me resolving error in WCF service which interacts with SharePoint lists. Thankx.

    ReplyDelete