I am going to explain my experiences while first time creating a console application in SharePoint 2010. It will be helpful for the beginners. I found few differences while creating applications in sharepoint 2010.
To know all the things what I have got please go through the below steps.
- Create a web application using CA(Central Administration).
- Add site collection and check whether it is working or not.
- In my example I have created an application in cloudshare (for more information visit my cloudshare post)
- I have created a site “http://sp2010:60000”. I have tested it is working fine
- Now I want to write a console application to list out all the lists in the sharepoint site which i have created.
- To create an application we need a visual studio 2010 only, below versions do not work for 2010 development.
- Open Visual Studio 2010
- Select ‘Visual C#’ and ‘Console Application’ template as below
- Enter Name and Click OK.
- Now console template will be appeared with the syntax code.
- Add reference “Microsoft.SharePoint.dll” by browsing the root folder of the sharepoint.(%systemdrive%\Program Files\Common Files\Microsoft Shared\\Web Server Extensions\14\ISAPI)
- It will add SharePoint assembly to the application.
- Now we can access the SharePoint objects in out coding.
- Add name space “using Microsoft.SharePoint”;
- Write code as below showing
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ShowLists
{
class Program
{
static void Main(string[] args)
{
SPSite site = new SPSite("");
SPWeb web = site.OpenWeb();
foreach (SPList list in web.Lists)
{
Console.WriteLine(list.Title);
}
}
}
} - Now execute the program.
- It would show an error as below, Please notice we have already added the Microsoft.SharePoint.dll file reference, even though it is showing an due to incompatibility with the .netframework which I have been selected(.netframework4.0).
- To rid this error please change the .netframework be following the below steps
- Go to the Project properties and change the .netframework version at Target Framework
- Change it to “.Net Framework 3.5”
- and build the program. It will build successfully.
- Now try to Execute the program by Run it.
- Again it would get an error as below
- I have checked earlier as it is existed but not found. This error is occurred because of that application is targeted to 32bit(x86) environment. Microsoft SharePoint 2010 does support 64bit(x64) only.
- So please change that target machine type as below, by clicking on the Build tab of project properties.
- Now Run the program. It will execute with the lists.