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条回答
做自己的国王
2楼-- · 2019-04-18 02:06

The FileNotFoundException is thrown by SharePoint when it cannot find the requested site collection in the SharePoint configuration database. My guess is that you have not yet created a site collection on the URL http://mysite:80. I see the following stack trace if I try and instantiate a new SPSite object with the URL of a non-existing site collection:

System.IO.FileNotFoundException : The site http://server/sites/bah could not be found in the Web application SPWebApplication 
Name=SharePoint - 80 Parent=SPWebService.
at Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSite..ctor(String requestUrl)

Specify the proper URL of your site collection or open Central Administration and create a new Site Collection.

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

We had this same Issue, but I am familiar with the different causes, here is a summary:

  1. You could have mis-typed or otherwise inputted the wrong address
  2. The User account running the process does not have the required permissions, which are: Read Permission to the SharePoint site, and a dbo of the SharePoint Config db, and Content db.
  3. The process must be a 64 bit process (the default is 64bit "Any CPU") when building on a 64 bit server.
  4. The process must be targeted at .NET 3.5
查看更多
手持菜刀,她持情操
4楼-- · 2019-04-18 02:07

It's also possible that the object model doesn't like the URL you're giving it. If you don't provide it with either the exact URL at which you created the site collection or an exact URL listed in your is configured in your Alternate Access Mappings, it will throw an exception that might not necessarily make sense. In your case you might try http://mysite or http://machinename.

查看更多
Juvenile、少年°
5楼-- · 2019-04-18 02:07

Stacktrace of the exception would be helpful.

I think you possibly can get some idea of what file it is and what is happening by disabling "just my code" in tools -> options -> debugging and looking at the filename argument in the call stack of the exception when the debugger shows it (if you can debug it of course), or maybe the name shows up in the exception message.

查看更多
【Aperson】
6楼-- · 2019-04-18 02:08

Check your web.config and see if there's a config there with a file missing.

Look in you 12 hive for the log. If your log settings are correct, you'll get the file missing.

EDIT: Check also if ALL your DLL are in the GAC. Check if your web.config file contains all the information: namespace,Classname, NameSpace, Version=version_number, Culture-your_culture, PublicKeyToken=your_signed_token

查看更多
来,给爷笑一个
7楼-- · 2019-04-18 02:08

I have recently discovered that this problem with the constructor can be cause by starnge behaviour of the constructor.
I'm taking about MOSS 2007. When you're passing a full site URL to the constructor, what it seems to do is to really consider only the site portion of the URL, choosing the web application which is 'currently seleced' in the web application selector control.
Thus, for example, when you have "http://webapp/sites/site" and have "http://weabapp:22345" currently selected (the last time you selected it in such a selector) when you call

SPSite site = new SPSite("http://webapp/sites/site")

It tries to actually create a site object for "http://webapp:22345/sites/site" and fails.

查看更多
登录 后发表回答