views:

9359

answers:

14

I try to instantiate a instance of SPSite on the farm server in a custom software (MyApp.exe) and I give in parameter the whole website (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. I always gives back "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  A: 

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.

axk
+1  A: 

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

vIceBerg
A: 

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.

slf
Nope. Everything is in the GAC.
Maxim
+2  A: 

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.

Lars Fastrup
I have a Web Application + Site collection that I can access through the browser.
Maxim
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.
Lars Fastrup
@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. :)
Maxim
+1  A: 

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.

Abs
I tried with a copy/paste of the string directly from the Alternate Access Mapping section.
Maxim
A: 

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.

axk
A: 

Did anyone find a solution for this problem. I face the same issue when I run the code

SPSite newSite = new SPSite("http://localhost:3333");

in a Unit Test, I get the following error.

Test method TestProject1.ProgramTest.MainTest threw exception: System.IO.FileNotFoundException: The Web application at http://localhost:3333 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application..

but when I run it in a console application, it runs gracefully :( btw I am using Vista 64bit Ultimate

http://www.wesam.info

A: 

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.

+2  A: 

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

Olof Szymczak
A: 

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

John Fields
A: 

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

Kiran
A: 

My issue is the same

When instantiating SPSite obj in code in a console application. The code runs without any errors.

When I use the same snippet of code to run in InfoPath code behind. It gives me the following error

System.IO.FileNotFoundException was unhandled by user code Message="the web application at .... could not be found. Verify that you have typed the URL correctly. If the URL SHould be serving existing content, the system adminsitrator may need to add a new request URL ,mapping to the intended application."

Stacktrace

at Microsoft.SharePoint.SPSite...ctor(SPFarm farm,Uri requstUri, Boolean contextSite, SPUserToken userToken)

VJ
A: 

I have also had a similar problem when the method that instantiates the SPSite is being run in an NUnit test. If I create a console or windows wrapper, it runs fine, but when it is called in an NUnit test, the same error is thrown. I haven't yet found a solution to this except to not use NUnit ...

Grady
A: 

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

charliedigital