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

Comments (36)

  1. Straight to the point!!

    nice one!!!

  2. anonymouscommenter says:

    I hit the point 5!

  3. anonymouscommenter says:

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

  4. anonymouscommenter says:

    Very useful, Ur blogs always provide the exaustive options 🙂

  5. Sagir Kazi says:

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

  6. anonymouscommenter says:

    Excellent post

  7. anonymouscommenter says:

    Very helpful. had a bunch to check.

  8. anonymouscommenter says:

    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.

  9. 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

  10. anonymouscommenter says:

    Very much helpful. Thanks

  11. anonymouscommenter says:

    I hit the point 5. it was a great help

  12. anonymouscommenter says:

    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]'

  13. anonymouscommenter says:

    Thanks Dana! Same problem for me 😉

  14. anonymouscommenter says:

    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

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

  16. anonymouscommenter says:

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

  17. anonymouscommenter says:

    Hours of searching solved by this post…

    Thanks!

  18. anonymouscommenter says:

    3rd point worked for me…Thanks 🙂

  19. anonymouscommenter says:

    Thanks! Bitness was the Problem for me

  20. anonymouscommenter says:

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

  21. anonymouscommenter says:

    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.

  22. Steve Xue says:

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

  23. ravneet singh says:

    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

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

      Cheers,
      Stefan

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

  25. john zachariah says:

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

  26. Gleb says:

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

  27. Perla Nava says:

    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

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

  28. Elvis Filipovic says:

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

  29. Thankful says:

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

  30. Phil says:

    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.

  31. Anurag says:

    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;
    }
    }
    }
    }
    }

    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

  32. 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]

Skip to main content