Sunday, March 14, 2010

Fix for Not able to connect to SharePoint 2010 server web sites through Visual Studio 2010

Today, I started working on SharePoint 2010 server side object model to delete all the files from the document library using batch command. I have written all the code but, I failed to connect to the web site using SPSite class. I tried all combination like with server name, ip address and many more. Always I am getting the exception that "not able to connect to SharePoint site".
This is the actual exception :
"The Web application at http://nb16 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."

Below figure gives you complete details.
Below is the code [Console application] I have used to communicate with the sharepoint server.
class Program
        static void Main(string[] args)
        private static void DeleteAllItemsUsingBatch()
            using (SPSite site = new SPSite("http://nb16"))
                SPWeb web = site.OpenWeb("/");
                SPList list = web.Lists["Documents"];
                StringBuilder sb = new StringBuilder();
                string batchCommand = "<method><setlist scope=\"Request\">" + list.ID + "</setlist><setvar name=\"ID\">{0}</setvar><setvar name=\"Cmd\">DELETE</setvar><setvar name=\"owsfileref\">{1}</setvar></method>";
                foreach (SPListItem item in list.Items)
                    sb.AppendFormat(batchCommand, item.ID.ToString(), item.File.ServerRelativeUrl);

                web.AllowUnsafeUpdates = true;
                web.AllowUnsafeUpdates = false;

Did you find any problem with the code? Not really. There are no problems with the code at all.. Then what's wrong?
When I researched on the net for the solutions I found below information.
1. Is SharePoint site running?
2. Did you develop code on the same SharePoint server?

But, everything fine. There are no issues. Then what was the problem? I was frustrated for 3 hours to find the solution.
It's simple that the platform target in build options was set to the x86 format by default. It should be set to x64 to work correct.
So, sometimes the simple problems will take more time to fix. Enjoy the nice series of posts on SharePoint 2010.


  1. Excellent. I am searching through net from morning and here is the solution.

    Thank you so much for sharing.

  2. Glad to hear that it helped for you... It was very tricky and it took around 3 hours to fix the problem for me.


  3. But don't you think to set it for 'Any CPU' is better then setting it only for x64?

  4. Hi Vikas,
    We always have the option to set it to Any CPU and that will work any way. But, in this case, I explicitly mentioned x64 only because, it is only works for x64 mode [SharePoint 2010]. So, I believe setting x64 is the best way to use it.

  5. Hi,

    This is the most obvious fix out there. Most of the people fall for that.
    Microsoft Playground

  6. Excellent. Had the same problem and fixed it! Thanks!