SP2010: SPFarm.Local is null or Web Application could not be found
Note: This post applies to SharePoint Server 2010 Beta 2 and Visual Studio Ultimate Beta 2.
I ran into two issues today that had me tied up for a few hours so I thought I would share. The first was simple:
SPFarm _farm = SPFarm.Local; However, the _farm object was being returned as null.
The second was again simple:
SPSite _site = new SPSite(“http://teams”); This line errored with:
The Web Application at http://teams
could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
First, notice that by default, Visual Studio compiles in x86. Since SharePoint is running in x64, this would definitely be a good place to start.
However, there is no x64 platform to compile to by default
What we need to do is create an x64 platform and copy existing settings from x86. We’ll do this by entering the Configuration Manager. Once in the Configuration Manager, select the platform drop down and choose New.
Choose the new platform to be “x64” and choose to copy settings from x86.
After you click OK, ensure the settings on your projects are change to use the new platform “x64”
Close out of Configuration Manager and ensure you are now building in x64.
Lastly, try your project again. That should have solved it.
Note: you may also receive this error if you do not have permissions to the configuration database while developing a Windows Forms or WPF application. Ensure that the account you are developing on has appropriate permissions to the config database. If you are running a web client (web part, etc.), ensure that its app pool account has the same permissions to the read from the database.
Special thanks to Tajeshwar for his help in resolve this.