14

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:

  SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            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

FarOoOosa
  • 398
  • 1
  • 4
  • 13
  • What are you developing? Console application? Event receiver? Web part? – Anders Rask Aug 06 '12 at 08:51
  • 1
    What URL are you using to connect to your site collection and what is the URL to your site collection? – Robban1980 Aug 06 '12 at 08:28
  • I said web application – FarOoOosa Aug 06 '12 at 09: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 06 '12 at 09:15

11 Answers11

16

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

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

Peter Maes
  • 161
  • 1
  • 3
3

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.

Vardhaman Deshpande
  • 10,462
  • 1
  • 26
  • 52
  • 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
3

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.

SPDoctor
  • 9,613
  • 2
  • 34
  • 60
bijayk
  • 131
  • 2
2

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.

1

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.

Naveen
  • 1,902
  • 3
  • 35
  • 64
1

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

Diptarag
  • 1,697
  • 1
  • 12
  • 21
1

change Platform Target to X64.

1

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

Regards,
JP

JP Roelofse
  • 511
  • 1
  • 3
  • 8
-1

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.

-3

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

Viraj Gorajia
  • 1,659
  • 1
  • 13
  • 26
-4

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

Metla
  • 1