How to: Create a Console Application
Applies to: SharePoint Foundation 2010
This programming task describes how to create a console application in Microsoft Visual Studio 2010 that displays the number of lists within a site collection.
Users must be administrators on the computer where a console application is executed in order to run the application in the context of Microsoft SharePoint Foundation.
To create a console application in Visual Studio
On the File menu in Microsoft Visual Studio, point to New and then click Project.
In the New Project dialog box, select a language in the Installed Templates box, and then select the Console Application template.
Type a name for the application in the Name box, and in the Location box, type the path to where you want the application to be created, and then click OK.
In Solution Explorer, right-click the References node, and then click Add Reference on the shortcut menu.
On the .NET tab of the Add Reference dialog box, select Microsoft.SharePoint, and then click OK.
In Solution Explorer, right-click the console application and click Properties. In the Project property page, select Application and set the target framework to .NET Framework 3.5, and then select Build and set the platform target to x64.
In the default .vb or .cs file, add a using directive for the Microsoft.SharePoint namespace, as follows.
Imports Microsoft.SharePoint
using Microsoft.SharePoint;
Add the following code to the Main method in the .vb or .cs file.
Overloads Sub Main(args() As String) Using siteCollection As New SPSite("http://Server_Name") Dim sites As SPWebCollection = siteCollection.AllWebs Dim site As SPWeb For Each site In sites Try Dim lists As SPListCollection = site.Lists Console.WriteLine("Site: {0} Lists: {1}", site.Name, lists.Count.ToString()) Finally If site IsNot Nothing Then site.Dispose() End If End Try Next site End Using Console.Write("Press ENTER to continue") Console.ReadLine() End Sub 'Main
static void Main(string[] args) { using (SPSite siteCollection = new SPSite("http://Server_Name")) { SPWebCollection sites = siteCollection.AllWebs; foreach (SPWeb site in sites) { try { SPListCollection lists = site.Lists; Console.WriteLine("Site: {0} Lists: {1}", site.Name, lists.Count.ToString()); } finally { if (site != null) site.Dispose(); } } } Console.Write("Press ENTER to continue"); Console.ReadLine(); }
Click Start on the Debug menu or press F5 to run the example.
See Also
Concepts
Working with List Objects and Collections
Using Visual Studio for SharePoint Development