Common issue: new SPSite API call returns "The Web application at http://server:port/ could not be found."

Since the early times of SharePoint we receive support cases where a customer receives the following error message when using custom code with SharePoint:

"The Web application at http://server:port/ 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"

Here are the most common reasons this error can occur:

  1. The code is executed on a different machine – The SharePoint object model (except the Client API) requires to be run on the SharePoint server itself. It is not possible to run the application on a server which is not within the same SharePoint farm the code is trying to access.
  2. Insufficient Rights on the site collection – The code is executed in context of an account which does not have read permission on the site collection
  3. Incorrect Url being used – Verify that the site works correct in a browser and double check that the server is correct registered in the AAM settings
  4. Incorrect bitness – The SharePoint object model needs to be executed with the same bitness as the operating system. That means you cannot use the SharePoint object model in a 32-bit application if the Operating System and SharePoint are installed as 64-bit version. Ensure to compile the project using the correct bitness (64-bit on a 64-bit machine vs. 32-bit on a 32-bit machine)
  5. Incorrect .NET framework version –  Ensure that the project is configured to use .NET 3.5 for SharePoint 2010 and below and .NET 4.0 for SharePoint 2013

37 Comments


  1. Straight to the point!!

    nice one!!!

    Reply

  2. Great post Stefan,
    I had the bitness wrong. Visual Studio 2010 set my console application to x86. I changed it to "Any CPU".

    Reply

  3. Very useful, Ur blogs always provide the exaustive options 🙂

    Reply

  4. #3 did the trick for me. One web application with multiple URLs. Adding to AAM solved the issue. Very helpful post!

    Reply

  5. Very helpful. had a bunch to check.

    Reply

  6. I also have one web application with different URLs. What does it mean by " double check that the server is correct registered in the AAM settings". I don't have access to central admin of the server as this is production environment and I am trying to
    deploy Sequential WF. Is there sth I am not doing correctly in my workflow as I am using workflowProperties.Web to get the web site whenever required.

    Reply

  7. Hi Nix,
    each Url used to access the relevant sharepoint web applicaiton either through the browser or the API has to be listed in an AAM zone.
    Cheers,
    Stefan

    Reply

  8. I hit the point 5. it was a great help

    Reply

  9. Thank you for the posts! Mine was a variation of #2 – my account did not have logon rights to the SQL Server database Sharepoint was using. I found this in the Event Viewer – hopefully it'll help others:

    SQL database login for 'SharePoint_Config' on instance '[xxxx]' failed. Additional error information from SQL Server is included below.

    Login failed for user '[yyyy]'

    Reply

    1. Thanks Dana Fillhart for your suggestions, checking the Event Viewer error log helps me to resolve the problem.
      Acutally, I have to create a login in SQL Server, and then grant access for this login to these databases: SharePoint_Config, Sharepoint_AdminContent_ ,

      Reply

  10. Thanks Dana! Same problem for me 😉

    Reply

  11. I am new to sharepoint and we have s sharepoint server and the .net application using SPSite object is in my local machine.
    I was trying to connect to the sharepoint site from my local machine using SPSite object and I got the "The Web application at http://server:port/ could not be found." error. Please let me know what are the things that I should check for the application to
    run. Thank you

    Reply

  12. Hi Jeff,
    please check the 5 points above.
    Cheers,
    Stefan

    Reply

  13. I experience for number 1. very helpful, thanks a lot.

    Reply

  14. Hours of searching solved by this post…

    Thanks!

    Reply

  15. 3rd point worked for me…Thanks 🙂

    Reply

  16. Thanks! Bitness was the Problem for me

    Reply

  17. Ditto for me on Dana's issue, thank you for posting! 🙂

    Reply

  18. Stefan, thank you very much! Your article avoids me hours of painful googling. I tried to execute server code (Microsoft.SharePoint.dll) on client machine without Sharepoint and couldn't understand why nothing worked.

    Reply

  19. Thanks Dana, points 4 and 5 fixed our issue. 🙂

    Reply

  20. I am new to Sharepoint and my question is if i am using the Central Admin URL to get SPSite my code works ,otherwise it shoots the above Exception

    Reply

    1. The Url needs to be the Url of the specific site collection you would like to connect to.
      Cheers,
      Stefan

      Reply

  21. Quite useful post, Jeff!
    Hit at point 3. Worked.

    Reply

  22. Thank you ! for me Incorrect bitness worked ! It saved my time 🙂

    Reply

  23. Please add 6. point – Need rights to Sharepoint Config Database.

    Reply

  24. Nice Post and to helpful! for me was the option 1. what can I do if I need to create SiteCollections from Farm1 to Farm2? Is it possible do it by Event Handler? please help I’m a bit lost

    Reply

    1. Hi Perla,
      I’m not sure what you are trying to achieve. Can you elaborate?
      Thanks,
      Stefan

      Reply

  25. Number 4, incorrect bitness was my issue. The reverse migration is back on track.

    Reply

  26. Great Post! Still helping out 5 years later! Thank You.

    Reply

  27. It is unfortunate that the error message does not seem to hint at what the issue is. It is like a person pointing to his foot to complain about a toothache.

    Reply

  28. I am working on document library. when I am moving file from sharepoint 2010 document library to another document library in sharepoint 2013. please help if you have any ide
    for both server is different but in same domain .how we can move from 2010 to 2013 programitically.
    My code is-
    using (SPSite site = new SPSite(“http://10.0.1.156/”))
    {
    using (SPWeb web = site.OpenWeb())
    {
    web.AllowUnsafeUpdates = true;
    SPDocumentLibrary library = (SPDocumentLibrary)web.Lists[“DMS”];
    SPFolderCollection folders = library.RootFolder.SubFolders;
    {
    foreach (SPFolder item in folders)
    {
    if (item.Name != “”)
    {
    SPFolderCollection ExistingMatters = item.SubFolders;
    foreach (SPFolder existingitem in ExistingMatters)
    {
    if (existingitem.Name != “”)
    {
    SPFolderCollection SharedFolder = existingitem.SubFolders;
    foreach (SPFolder sp in SharedFolder)
    {
    if (sp.Name== “Shared”)
    {
    SPFolder oldFolder = web.GetFolder(library.RootFolder.Url + “/” + item.Name + “/” + existingitem.Name + “/Shared”);
    SPFileCollection folderColBilling;
    folderColBilling = oldFolder.Files;
    foreach (SPFile file in folderColBilling)
    {
    using (SPSite Dest = new SPSite(“http://clientinterface:27213/”)) — here problem occurs
    {
    using (SPWeb Dweb = Dest.OpenWeb())
    {
    web.AllowUnsafeUpdates = true;
    SPDocumentLibrary libraryDest = (SPDocumentLibrary)web.Lists[“Documents”];
    byte[] fileBytes = file.OpenBinary();
    SPFile d = libraryDest.RootFolder.Files.Add(file.Name.ToString(), fileBytes, true);
    d.Item[“FileName”] = item.Name;
    d.Update();
    }
    }
    }
    }
    }
    continue;
    }
    }
    web.AllowUnsafeUpdates = false;
    }
    }
    }
    }
    }

    Reply

    1. Hi Anurag,
      this code cannot work. The code uses server side API. Means API which only works on the SharePoint server itself. You cannot reach a remote SharePoint server using server side API.
      Using server side code you need two applications:
      one running on the 2010 server which downloads the document and copies it to (e.g.) a file share and a second one running on the 2013 server which uploads the file form the file share to SharePoint.
      Alternativel you can look into client side API.
      Cheers,
      Stefan

      Reply

  29. We had a console application which is run by Windows Scheduled task, and it was unable to open a document library.
    1. Code was executed on SharePoint 2013 Server
    2. Adding Site Collection Admin permissions or even Full control via Web Application policy did not help. However, when we tested to logon to server, user was able to open the document library with browser, so the user had sufficient access.
    3. The URL was correct, user was able to open the document library with browser
    4. Build Platform was set to Any CPU (prefer 32-bit unchecked). We also tested x64, no effect
    5. .NET Framework target was 4.5.2. We also tried to downgrade to 4.5, no effect.
    The only thing which seemed to help were these:
    # gives access to config db, user is programmatically able to open SPSite-object
    Add-SPShellAdmin -username [domain]\[username]
    # gives access to content db, user is programmatically able to open SPWeb-object
    Add-SPShellAdmin -username [domain]\[username] -database [content db guid]
    You can get the Guid of content db by running following
    Get-SPContentDatabase -webapplication [web application url]

    Reply

Leave a Reply to Stefan Goßner Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.