3

We have a mixed (forms and Windows)-based SharePoint 2010 site and I'm attempting to use the object model in c# (command line console app, referenced to Microsoft.SharePoint, Solution Platform target is x64 [in VS 2012 I had to create a new solution platform, settings copied from <Empty>].)

In PowerShell, I can simply do something like

$site = Get-SPSite http://www.server.com/sites/site1

It successfully retrieves the object, and I can open the contained SPWebs, etc. as I please. However, when I attempt similar logic in C# (on the same box under the same user credentials) like

using(SPSite site = new SPSite("http://www.server.com/sites/site1") { }

it fails with the FileNotFoundException. Any attempts to use the associated Guids follow the same issue. Why might this be happening?

Also, I am an administrator on the box and also explicitly have sysadmin server role on the database.

Michael
  • 1,786
  • 5
  • 23
  • 42

2 Answers2

3

I assume you are doing this in a command prompt? If so, please make sure you are compiling your project in x64. This is the error message I see when I compile my code in x86 or Any CPU.

Chris Auer
  • 1,405
  • 12
  • 23
  • Thank you @dnstommy; Upvoted for future visitors that might not have done this. I had already set the platform target to x64, as I ran into earlier issues using Any CPU. I am building this as a new Console Application in Visual Studio 2012 Ultimate. – Michael Dec 19 '12 at 16:14
0

For anyone experiencing this as well. PowerShell 3 does not support SharePoint methods. I discovered after installing Powershell v3 that I had to enter another shell using the command powershell -version 2 first before running SharePoint commands, which led me to realize that my VS project was targeting .NET 4.5; Once I changed it to .NET 3.5, everything worked.

I assume that despite the SharePoint libraries targeting .NET 3.5, and theoretically the .NET 4 CLR can handle .NET 2 CLR libraries, Projects with usage of SharePoint 2010 do not support targeting .NET 4/4.5 in any way.

Happy to be proved wrong, of course ;)

Michael
  • 1,786
  • 5
  • 23
  • 42