I'm trying to use SP 2010 site from a web application and when I create a new instance of SPSite I get this error: 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 administrator may need to add a new request URL mapping to the intended application. I know that this question is common over the net but I didnt find the solution for me The Code:

            using (SPSite site = new SPSite("http://servername:port"))
                elevatedWeb = site.OpenWeb();

1.I run my code on sharepoint server its the same machine

2.I copy paste the Url to browser and its work

3.I use .NET 3.5 Framework

4.I have full permission on site for administrator account and also dbowner

5.I used platform target Any CPU my sharepoint is 64 and when Im try to make it 64 i face a problrm with my Dll: "Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format."

Any Suggestion for that big problem

  • 1
    What URL are you using to connect to your site collection and what is the URL to your site collection? – Robban1980 Aug 6 '12 at 8:28
  • What are you developing? Console application? Event receiver? Web part? – Anders Rask Aug 6 '12 at 8:51
  • I said web application – FarOoOosa Aug 6 '12 at 9:12
  • I try it on seperate application and its work after change to 64 for platform target but how to work with that exception "Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format" – FarOoOosa Aug 6 '12 at 9:15

11 Answers 11


Change the build options in your project: Platform target: any cpu.

Rebuild your project and you should be able to access it.

| improve this answer | |
  • Changing "Platform target: any cpu." helped.. Thanks! – user14822 Feb 11 '13 at 6:55

All the answers which suggest logging in through the Admin account for development pose a serious risk because that is a very bad practice. You will rarely see a production environment which has been configured this way.

I will suggest that you give only the required privileges to the user with whom you are going to deploy your solutions.

| improve this answer | |
  • 1
    Not just ANY Admin, but a server FARM admin. AND DBO. AND Site collection Admin. I just want to upload a document, what the merciful christ do I need that many permissions? – Michael Blackburn Jun 24 '16 at 15:47

I found that it works in a console application, but when I use this piece of code with an ASP.NET application it throws IO exceptions. Since there are some 32bit dlls referenced with my ASP.NET application, this creates a problem when I try to change to platform target of 64 bit.

For resolution, I created a separate service application running in a different process all the time. Luckily I had a scheduler kind of requirement so this workaround helped me.

| improve this answer | |
  • Seriously?! I'm in the same sitiuation :( – David Sep 3 '15 at 12:11

I found that adding my user id to "WSS_ADMIN_WPG" did NOT fix the problem when in debug mode. I had to run Visual Studio 2010 in Administrator mode also.

| improve this answer | |

Please run the code from system administrator account if possible, otherwise add yourself to WSS_ADMIN_WPG group and ensure you are building the project with correct version i.e. 32-bit or 64-bit

| improve this answer | |

change Platform Target to X64.

| improve this answer | |

You need to run the application build as Any CPU, and add 2 references,

  • Microsoft.Office.Server.dll
  • Microsoft.SharePoint.dll

Both can be found in your ISAPI folder


| improve this answer | |

Add-SPShellAdmin -UserName domain\username did the trick for me. The documentation says that it adds user to the SharePoint_Shell_Access role in the farm configuration database only, and also ensures the user is added to the WSS_Admin_WPG local group on each server in the farm.

| improve this answer | |

The above items are most probable - but:

Check the name and URL given to the site in the SharePoint Central Administration - Alternate Access Mappings (AAM). Use the URL mapping given in here.

| improve this answer | |

close visual studio 2010, and open it again by right clicking on it and selecting “Run as Administrator".

| improve this answer | |
  • are you sure this will work? because as i tried to do it in my system it didn't worked. – Torque Oct 17 '12 at 11:59

try to executive your code by using SharePoint Admin account...

| improve this answer | |
  • how to use that for admin account – FarOoOosa Aug 6 '12 at 8:43
  • 6
    Not very specific. Please improve this answer. – Anders Rask Aug 6 '12 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.