FileNotFoundException with the SPSite constructor
Asked Answered
I

21

22

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.

Illfavored answered 5/11, 2008 at 19:22 Comment(2)
Can you put your stack trace?Entertain
I fixed my problem but I don't know what caused it.Illfavored
O
14

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.

Outmoded answered 5/11, 2008 at 19:42 Comment(4)
I have a Web Application + Site collection that I can access through the browser.Illfavored
hmm, then it might be a permission issue. Try running your app with the same credentials as the application pool instead of the site collection admin. Otherwise the problem could also be because your SharePoint is 64-Bit but your app is running in 32-bit mode.Outmoded
@iaf : it was a permission issue. If you are running an EXE with a "unknown" user to SharePoint, that user needs to be dbo of the content DB + Farm Admin + Site Collection Admin. That's all. :)Illfavored
My issue was that I was calling 64-bit SharePoint 2010 assemblies, but running in 32-bit process. Thanks @lars-fastrup!Joselynjoseph
F
12

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

Flown answered 5/10, 2010 at 20:2 Comment(1)
This did the trick for me as well. Thanks Charliedigital. As usual, SharePoint gives a very unhelpful error message.Chromosphere
C
10

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!!!

Cauldron answered 7/10, 2009 at 7:29 Comment(1)
This turned out to be my issue. You need to be careful to check the CPU configuration for each project, it can be a bit obscure.Truculent
A
4

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

Amphetamine answered 3/12, 2010 at 22:13 Comment(0)
M
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.

Matelot answered 28/1, 2011 at 5:17 Comment(0)
B
2

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.

Brittain answered 5/11, 2008 at 19:44 Comment(1)
I tried with a copy/paste of the string directly from the Alternate Access Mapping section.Illfavored
E
1

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.

Entertain answered 5/11, 2008 at 19:27 Comment(0)
P
1

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

Pintail answered 5/11, 2008 at 19:27 Comment(0)
E
1

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.

Entertain answered 20/12, 2008 at 11:56 Comment(0)
V
1

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.

Viki answered 3/2, 2011 at 13:9 Comment(0)
T
1

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!

Tertial answered 3/11, 2011 at 2:8 Comment(0)
K
1

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
Kino answered 21/1, 2014 at 21:14 Comment(0)
G
0

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.

Gaulish answered 5/11, 2008 at 19:34 Comment(1)
Nope. Everything is in the GAC.Illfavored
B
0

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.

Benita answered 15/6, 2009 at 21:24 Comment(0)
A
0

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

Apocopate answered 3/11, 2009 at 18:34 Comment(0)
I
0

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

Induration answered 2/12, 2009 at 21:37 Comment(0)
S
0

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.

Sclater answered 30/1, 2013 at 16:8 Comment(0)
S
0

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.
Siderostat answered 19/5, 2014 at 21:10 Comment(0)
G
0

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.

Galantine answered 25/11, 2014 at 11:51 Comment(0)
K
0

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

Kolyma answered 10/4, 2015 at 2:54 Comment(1)
Yet another reason why I'm not a fan of SharePoint. Dark magic everywhere.Illfavored
M
0

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.
Magnetochemistry answered 19/1, 2019 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.