Sunday 8 July 2012

FileNotFoundException on SPSite using Visual Studio 2010

So you now jumped into SharePoint 2010 custom development. Today you have probably written your first custom SP application using Visual Studio 2010 and tried accessing SharePoint 2010 using Server object model. You would have probably grabbed a code snippet from Internet (if you are a beginner) and pasted in VS 2010 IDE, built you application successfully (you probably would have taken a sigh of relief and might have also assumed that –yeah I have done it) and with all enthusiasm you would have hit F5 to see the output.

But oops, what happened in very next second. Your smile has suddenly gone into vain as you end up encountering of “File not found exception”. Isn’t it?
Now you have started debugging and found that you are getting this error when trying to create an object of “SPSite” class.

You would have thought, so what, let me quickly Google to find the fix. (By the way, I did the same as well :))
You would have at least relaxed a bit by seeing umpteen Google results and in thinking that you are not the loan sufferer.
Most likely you would have applied the common fixes mentioned below but still no relief:
  1. Checked and changed the Target Framework to .NET Framework 3.5, if this wasn’t the case
  2. Checked and changed “Target Plateform” to ‘Any CPU’ or ‘x64’

What to do now?

Don’t worry, i will try to illustrate and hopefully you will be able to fix this issue.
Let’s have a quick walkthrough of What, When and Why do we get this expectation before jumping to fix it

What

You are getting an exception like:

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



Exception Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.


Exception Details: System.IO.FileNotFoundException: 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.”


Stack Trace: FileNotFoundException: The Web application at …….

When

You are trying to access SharePoint 2010 Server object mode using Visual Studio 2010 and getting this exception at ‘SPSite’ object creation step.





You could do various types of custom developments (e.g. console based, Window based, web based, service apps, Timer jobs, feature type development etc) but most likely you will encounter this issue when you are developing:


  • a console/windows application and trying to access SharePoint 2010 server object model
  • a web application and trying to access SharePoint 2010 server object model

Why

Now, as you know SharePoint 2010 runs on an x64 platform and requires .NET Framework 3.5. Hence you must adhere to same environmental settings. The development environment is essentially combination of IDE (VS 2010) and IIS.

So if you try to access the SP 2010 object model from VS 2010, where your environment is setup for x86 as platform/target platform and .NET Framework 4.0 within VS, you will encounter this issue.

Configuration steps

Here comes Fix.

Accessing SharePoint 2010 Server object model with a console application

Follow below steps:

1. Run Visual Studio as Administrator. I recommend, to find Visual Studio exe ,which is “devenv.exe” (default location for exe is “C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE”) and do right click->Properties-> Compatibility -> Privilege Level-> Tick/check to ‘Run this program as an Administrator” -> OK. Now VS 2010 will by default run as Admin.
2. Recheck and ensure that your project environment is setup to use .NET Framework 3.5.How: Right click to your console/windows project-> Properties -> Application tab -> Ensure you have ‘.NET Framework 3.5’ under Target framework:

3. Recheck and ensure that Target platform is set to “Any CPU” or “x64”. How: Right click your project -> Properties -> Build -> Platform Target -> Ensure you have either “Any CPU” or “x64” specified
Or
That’s it. Save project , rebuild project/solution or hit just hit F5. The “FileNotFoundException” exception should have gone.

Accessing SharePoint 2010 Server object model with an ASP.NET application

Ensure that you have followed all the steps as we saw before with the console/windows application.
Now we need to check and perform few more steps as mentioned below:
1. Check and make a note of your SharePoint 2010 web application is running under which application pool and that app pool is running under which identity?

2. Ensure that the user account for the app pool has enough rights to databases on SQL Server. Db_owner permission on SharePoint_ContentDB(site collection database) and SharePoint_ConfigDB.

3. Ensure that the user account for the app pool is a member of the WSS_ADMIN_WPG group on the SharePoint server

4. Ensure that the user account for that app pool has got minimum of Read permission to the SharePoint site

5. Check whether you are using “visual studio development server” of VS 2010, if so then this is the most likely culprit for the exception.

Why so?

Because this integrated web server of VS 2010 does not run in x64 environment. May be Microsoft will get this improved in upcoming version of Visual Studio. But what to do for now?

Don’t worry! We can change our ASP.NET project configuration to be debug gable in an external web server such as IIS7. To do so, we need to follow these steps:

1. Ensure that you have performed all the steps as mentioned above
2. Make a note of user account for application pool which your SharePoint 2010 web application is using. Also create a local folder; this will become your virtual directory for the web application we are creating in next step. Let’s create a folder named “MySPApp” in C drive of your computer. (C:\MySPApp). I am also assuming that user account for app pool is let’s say “SharePoint - 80”
3. Create a web application in IIS7 (Note: Steps and screen shot given below are mainly for beginners, rest can just grab the idea and feel free to configure as your own choice).
4. Follow the screen shots below to create a web application. Note I am given port number 78, just to avoid any conflict with default web app :
5. If you are using app pool other than one being used for your SharePoint web app then ensure to configure that application pool to run for the .NET Framework Version v2.0 and x64.
6. To ensure that the application runs in x64, just set “Enable 32-Bit Applications” to false.
7. configure your web application in Visual Studio 2010 in the property page to “Use Custom Web Server” and specify there the url of your web application we created in step 4:

8. Publish (right click on project -> Publish) the .NET web application to the target web application of IIS7. I am using local file system to publish , you may choose of your preference
That’s it. Our environment for custom SharePoint application development is ready. After pressing the F5 button, Visual studio 2010 will automatically attach the debugger to the right IIS7 worker process.
Keep your figure cross and hopefully you should NOT be getting “FileNotFoundException” any more.
Hope this helps. Happy development and debugging :)

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

Exception Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: 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.

Stack Trace: FileNotFoundException: The Web application at ..etc"

4 comments:

  1. Hi!

    I see that this is your first post! I'm Happy to know that it's about: "FileNotFoundException"... =P.

    I was having the same problem. I read your instructions and I made the changes. The problem it´s gone.

    Go ahead with your blog!... Thank you!

    ReplyDelete
  2. Hi Jeet,
    Thanks for your article.

    I faced same error and your blog solved my issue.
    Regards
    Pradeep

    ReplyDelete
  3. Thank you very much. I have to spend 2 days without figuring out the issues.
    This article is very helpful.
    Peter

    ReplyDelete
  4. Even if I set the framework to 3.5 and Target to AnyCPU I get the file not found exception when executing the console app on another server. It works well on my development machine. Any Idea?

    ReplyDelete