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"

Monday 9 February 2009

What is wrong in Living a Simple and Happy Life?

The recent changes have brought in fear and worry in most of the people on how to manage the coming years.

Last 5 to 6 years, the importance of using money properly has lost its relevance. Just blindly copying unwanted culture has brought in high expectations, expenditure beyond proportion and artificial inflation across society.

The recent incident in December has made everyone to talk about the greediness of few individuals and also about the methods used to grow the company. Though I am not justifying the same, I honestly want to ask the question, what moral standards do all of us have to talk about it?

Most of the people (IT and other high earning Employees) have borrowed loans, 100% loan amount, for houses and Cars varying between 60 Lakhs to 1 Crore, paying EMI in the range of Rs 30K to 60K. Everyone assumed that the external world will go on like that. For those who got stocks, the expectation was that the stock value would grow in multiples within 1 or 2 years. Last few years the expectation of stock market is also the same. Everyone knows it is like gambling (simple mathematics knows it is not possible for it to grow) but still wanted to play around and make easy money.

Fundamentally the greediness in everyone has brought in the current state of affairs across the world and hence STOP blaming anyone for the same except our own selves.

What is the best all of us can do at this point of time? Let us all go back and look at our parents & ancestors and see how they lived life. Most of them lived a very simple and contended materialistic life and they were very happy with it.

Look at Abdul Kalam and other few great leaders. They are living a very simple life in spite of being popular and can afford a luxurious life. My father a lawyer by profession at the age of 81 still goes to the court by auto. Even in Wipro few of us, who are working for more than a decade live in rented houses and use only compact cars. The excess money is being donated for social benefit.

People like them are all passionate about the Job that they are doing and that passion alone is making them to grow stronger and stronger. Instead of choosing a career that will pay more money or focusing on external materialistic things alone, be passionate about the career or the Job that you are doing and that will give immense satisfaction.

The world is not going to be same again, it will still go through a lot of changes but let’s us not worry and fear. If you look at the history, crossing several thousands of years, right from Satya yuga, Thretha yuga, Dwapra yuga and the current Kali yuga, even great people and leaders went through difficult times. Everyone has to go through a difficult time however great he or she is because Nature wants to cut the ego of every Human being and make them humbler and simpler. There are no exceptions and hence be ready to learn from the experience of difficult times.

There is nothing wrong in leading a Simple life - loan free and minimum expectation - makes one to enjoy every moment of life. Even if you have to take a loan, at the time of need, follow the 80:20 rule, 20% is loan and 80% your own investment. Even if you have taken a big loan, at worst situation, without bothering about the family circle etc, be ready to walk away without being possessed or sentimental about the item.

Success is not in materialism, Success is not what position or status you have in your Company or Society.

Real Success is if you can be at peace within yourself in every moment of your life.

Every Day Practice Silence for 10 minutes. It will slowly change you from an animalistic nature to being a Good Human being, caring & sharing for others. Finally it will take you to the Divine nature, Truth / Ultimate Silence, where you are aware that you are everything.

As Guruji Viswanath always says “Life is to be lived, respected and celebrated”

May God Bless you all to get the knowledge of True Wisdom and to lead a really Happy and Peaceful Life.

Source : Email from a senior manager of a leading IT company