FileNotFoundException with the SPSite constructor

Maxime Rouiller picture Maxime Rouiller · Nov 5, 2008 · Viewed 33.7k times · Source

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.

Answer

Lars Fastrup picture Lars Fastrup · Nov 5, 2008

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.