FileNotFoundException with the SPSite constructor

2019-04-18 01:42发布

I try to instantiate an instance of SPSite on the farm server in a custom process (MyApp.exe) and I give it as parameter the whole URI (http://mysite:80/). I also made sure that the account running MyApp.exe is Site Collection Administrator.

However, I can't make an instance of SPSite whatever I am trying to do. It always throws a FileNotFoundException.

Anyone got an idea?

StackTrace:

at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl) at MyCompanyName.Service.HelperClass.GetItemStateInSharePoint(SharePointItem item) in C:\Workspaces\MyCompanyName\Development\Main\MyCompanyName.SharePoint\Service\HelperClass.cs:line 555

Another side note... I have a Web Application + Site collection that I can access through the browser without any problem.

21条回答
The star\"
2楼-- · 2019-04-18 02:09

I had the same problem, I made below changes and it started working.

  1. Changing the platform target in visual studio to x64
  2. make sure you are running visual studio in "Administrator" mode.
查看更多
地球回转人心会变
3楼-- · 2019-04-18 02:10

Changing the platform target in the build properties to x64 solved this issue for me on SharePoint 2010.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-18 02:10

You can keep the project compilation target set to "Any CPU". The important this is to configure the MSTest host process to run in 64bits. Open your .testsettings file, go to Hosts tab and set "Run tests in 64 bit..."

If after this when you run your tests VS tells you there aren't any, remove and add your test project again (I don't know a better workaround for this)

Hope it helps!

查看更多
Luminary・发光体
5楼-- · 2019-04-18 02:11

I had the same issue. I wanted to run console application with my user id. I am web application owner + Farm Admin. Still was not able to run the application.

Issue was resolved by

  1. Changing the platform target in build properties to x64

  2. In site settings -->Users and Permissions --> Site Collection Administrators there were two names. Removed other name and it started working.

查看更多
仙女界的扛把子
6楼-- · 2019-04-18 02:12

The MSTest on x64 issue was the cause of this problem for me. Works in a console app.

查看更多
霸刀☆藐视天下
7楼-- · 2019-04-18 02:13

Switching to NUnit might not be an option for everyone.
In my case, the problem was that I was on a 64 bit server, I had Any CPU checked (so it was picking the correct version) but my Test Settings were set to "Force tests to run in 32 bit process" (GAH!)

In MSTest, Go to TEst->Edit Test Settings->Trace and Test Impact.
Choose Hosts.
Make sure you are running against the correct version. Here's what you should be picking

Here is my checklist for VS2010 SP1, MSTest.

  • You need SP1 so that you can target tests to .NET 3.5. It will not work with .NET 4.0
  • Make sure the site loads- i launched the site directly from the VS2010 editor, since it's a hyperlink
  • Verify build settings. To Choose 64 bit if server is 64-bit.
  • In my case I had a 64 bit server, but choosing x64 would fail! That was my first clue.
  • Verify that the test settings support the correct bits.
查看更多
登录 后发表回答