SPFarm.Local is null?

It took me some time to find the solution to a problem, where a WPF application could not connect to a local SharePoint farm via SPFarm.Local. It always returned null.

Windows 7 was not the problem. I copied my application to a Windows Server 2008 VM to test.

In case somebody has the same problem, here is the solution:

image

Make sure you have set the Platform to “Any CPU” and not x86 if you are using x64 assemblies.

Update:

SPFarm.Local wants to create a connection to the configuration database. In order to do so, you’ll need to have permissions on the configuration database. Usually the service account and the application pool accounts can read the configuration database.
If you don’t have the right to read the configuration database, SPFarm.Local will be null!

The same permissions are required if you want to access SPAdministrationWebApplication.Local

Update2:

There is an entry on connect. You can vote for “Any CPU” as default target:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=455333&wa=wsignin1.0