可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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.
回答2:
Changing the platform target in the build properties to x64 solved this issue for me on SharePoint 2010.
回答3:
Read this site http://community.bamboosolutions.com/forums/t/8179.aspx if you are running your OS x64 bit and are using MSTest (32bit) it will fail, use nunit works!!!
回答4:
If it is a console application accessing SharePoint 2010 you make sure your project's Build Target is x64 and the .NET Framework is 3.5.
回答5:
This Issue is more of user permission issue give the following permission
User Permission
SharePoint Site --- Minimum Read Permission
Sharepoint Server --- Add to WSS_ADMIN_WPG group
Database --- Sharepoint Content DB (Site collection database) - db_owner permission
Sharepoint Config DB (Config DB of sharepoint installation) - - db_owner permission
Read more in my blog
http://sharepointinstallation.blogspot.com/2010/12/minimal-permission-required-to-execute.html
回答6:
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
.
回答7:
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.
回答8:
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
回答9:
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.
回答10:
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
Changing the platform target in build properties to x64
In site settings -->Users and Permissions --> Site Collection Administrators there were two names. Removed other name and it started working.
回答11:
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!
回答12:
We had this same Issue, but I am familiar with the different causes, here is a summary:
- You could have mis-typed or otherwise inputted the wrong address
- 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.
- The process must be a 64 bit process (the default is 64bit "Any CPU") when building on a 64 bit server.
- The process must be targeted at .NET 3.5
回答13:
I was plagued by this a few weeks ago. Ultimately I discovered that the file that could not be found was the SharePoint assembly itself. The runtime was failing to load the satellite assembly via late-binding.
The solution to my problem was to register the SharePoint 12.0.0.0 Assemblies in the GAC. It doesn't sound like it's the same as your problem, but just FYI.
回答14:
We faced the same problem some days ago, and the solution was to set the application, that is trying to create de SPSite object, to use the same AppPool that the Sharepoint's web application.
Hope it helps.
回答15:
The MSTest on x64 issue was the cause of this problem for me. Works in a console app.
回答16:
I have similar kind of issue.
In my scenario, I was able to create the instance of SPSite from a console application, but when another team mate tried to do so, the application threw the same exception as mentioned above.
Solution: I added the other teammate as administrator on Content Db server box (this may not be possible for everyone), the code works fine and no error
回答17:
Same problem on SharePoint 2010. However the issue was with our web service which was accessing the SharePoint object model. The app pool that this service should run under should be a farm admin.
回答18:
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 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.
回答19:
I had the same problem trying to access Sharepoint 2010.
I fixed it by changing the Target Framework to .NET 3.5.- which is the supported version for Sharepoint 2010.
回答20:
In my case it was definitely a permissions problem with the account I was logged into windows with.
Try this command in SharePoint Management Shell running as Administrator:
Get-SPSite 'http://yoursite/yourcollection'
If you get errors then logon to the SharePoint server as the app pool user or the account used to install SharePoint and try the above command again.
If it works then you know your previous account has a permissions issue. To fix the problem, run this command in the same shell window and supply the account you want to use in VS:
Add-SPShellAdmin -UserName Domain\User
回答21:
I had the same problem, I made below changes and it started working.
- Changing the platform target in visual studio to x64
- make sure you are running visual studio in "Administrator" mode.