Tuesday, June 14, 2011

Things To Look Out For While Developing SharePoint 2010 Server Applications

As part of mastering SharePoint Server 2010, I was trying out a simple console application which utilize's SharePoint Server side API's to communicate with SharePoint Server 2010. The console application which I was developing was programmed to fetch all the Lists in a given site collection and display their names(Titles), a pretty simple app.

For this I created a new console application in VS.NET 2010 by selecting ".NET Framework 3.5" from the drop down located in "New Project" window.




After which I added a reference to "Microsoft.Sharepoint.dll", which can be found under ".NET" tab of "Add Reference" window(to get here right-clicking "References" folder in solution explorer and select 'Add Reference' from the context menu).

Thereafter I updated the "Main" method with the following code, which does fetch all the SharePoint Lists in the default site collection available under "http://SP2010" (this URL refers to my default SharePoint site collection on my machine)

string SiteURL = "http://sp2010";

using (SPSite myspsitecollection = new SPSite(SiteURL))
{
  using (SPWeb mySPSite = myspsitecollection.RootWeb)
  {
    Console.WriteLine(mySPSite.Title);
    foreach (SPList list in mySPSite.Lists)
    {
       Console.WriteLine(list.Title);
    }
  }
}

The compilation went well without any glitches. The moment I ran this application, I got an error message stating that, the console app couldn't find the site. The exact error message was "The Web application at http://sp2010 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 was wondering why on earth my console application can't find this site. At the same time all other apps like SharePoint Designer and SM2 (SharePoint Manager 2, a third party utility) all of which were able to successfully connect to my site from the same machine. It was really embarrassing situation trying to troubleshoot like, why only my application wasn't able to establish the link. To get my console app on track, I tried disabling  the firewall and IDS, rechecked the code, installed Fiddler to monitor the requests to the web server etc, but nothing turned out to be providing hint of any sort to the root cause of this scary bug. As last resort I thought of searching for this error on the internet. After going through a couple of links, I came to hit the bulls-eye, finally got the culprit.

Solution:
The error was because of a default setting in VS.NET 2010. Whenever you create a new project in VS.NET 2010 the project by default sets the "Target Platform" to x86. So if you are developing Sharepoint Server side solutions, always make sure that you set the "Platform Target" value to "x64", else you will be getting weird errors, like the one I got. After updating my  console applications "Platform Target" to x64 things went smoothly as expected, listing out the titles of all the Lists in the given SharePoint site collection.



So to summarize; while developing server side apps for SharePoint 2010 make sure you follow these steps without fail.
  1. Always select ".NET Framework 3.5" from the drop down while creating new project. This applies to both Server Site app and Client side apps.
  2. Update the "Platform Target" value to x64, as SharePoint 2010 is 64-bit product.
  3. Refer "Microsoft.SharePoint.dll"
  4. Make sure you are able to connect to the Sharepoint site by specifying the url in Sharepoint Designer. If not make sure that no firewall policies are preventing you from connecting to sharepoint server.
  5. Your application will be running under the same login credentials with which you had logged into the system.



Happy SharePoint development.

No comments: