Error: The Web application at http://localhost/ could not be found

Ok, so I’ve run into the first (small) problem. As I was working on a small tool (which is supposed to solve another small problem I ran into), which retrieves all lists in a certain web, I immediately ran into a problem. Figures, problems with the first line of code. Good excuse to post some code and try this wp-plugin. As I am pretty new to this SharePoint coding, I am open for suggestions and tips and will adjust articles and code accordingly.

 C# |  copy code |? 
            using (SPSite site = new SPSite("http://localhost/"))
                 using (SPWeb web = site.OpenWeb(""))
                     foreach (SPList l in web.GetListsOfType(SPBaseType.GenericList))
                         Guid myGuid = l.ID;
                         string strName = l.Title;

I got the following error message:

The Web application at http://localhost/ 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.

The solution was very simple, the target platform is x64 while a new project by default targets x86. So go to the menu Build -> Configuration Manager and change the active platform to x64.

3 thoughts on “Error: The Web application at http://localhost/ could not be found

  1. Two things:
    1. In my experience, SharePoint doesn’t support generic names and/or IP addresses – you need a hostname or fully qualified domain name. Fortunately it is easy to do this on SharePoint simply simply by using the computer name instead of localhost. A fqdn is a nice touch but requires more configuration – especially on Windows 2008.
    * if you do use a fqdn or a hostname and you are using a network proxy you will probably have to add those same names to your proxy exclusion lists
    * And you will need to add them to your windows hosts file in C:\Windows\System32\drivers\etc by simply adding a new entry with the loopback address and the hostname
    * Finally if you are using Windows 2008 you will either have to disable the loopback check by adding a new key or by adding the new hostname to the BackConnectionHostNames in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0

    2. Get to know PowerShell! You can test these concepts out quite easily without having to go through the build, compile, deploy cycle e.g. $spsite = Get-SPSite http://myboxname

    Good luck!

    • Thanks for the reply. The localhost reference is a random reference for this example, I replced the actual URI. Furthermore, I agree with you that PowerShell is a must for every administrator but simply not an option for everyone. If you’re a site collection administrator you do not always have PowerShell at your disposal.

    • In addition to this answer.

      The account you are running on should have read/write (common practice to give db_owner) permissions on the specific Content, Administration and Config Databases.

Leave a Reply

Your email address will not be published. Required fields are marked *