Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
SDK Documentation
General Reference
 How to: Create a Console Applicatio...
Community Content
In this section
Statistics Annotations (3)
This page is specific to
The 2007 product release

Other versions are also available for the following:
How to: Create a Console Application

This programming task describes how to create a console application in Microsoft Visual Studio 2005. The example displays the number of lists within a site collection.

Users must be administrators on the computer where the script is executed in order to run a console application in the context of Windows SharePoint Services.

To create a console application in Visual Studio 2005

  1. On the File menu in Visual Studio 2005, point to New and then click Project.

  2. In the New Project dialog box, select a language, and then select Windows in the Project Types box.

  3. In the Templates box, select Console Application.

  4. In the Location box, type the path to where to create the application, and then click OK.

  5. In Solution Explorer, right-click the References node, and then click Add Reference on the shortcut menu.

  6. On the .NET tab of the Add Reference dialog box, select Windows SharePoint Services in the list of components, and then click OK.

  7. In the .vb or .cs file, add a using directive for the Microsoft.SharePoint namespace, as follows.

    Visual Basic
    Imports Microsoft.SharePoint

    C#
    using Microsoft.SharePoint;
  8. Add the following code to the Main method in the .vb or .cs file.

    Visual Basic
    Overloads Sub Main(args() As String)
    
        Dim siteCollection As New SPSite("http://Server_Name")
        Dim sites As SPWebCollection = siteCollection.AllWebs
        Dim site As SPWeb
    
        For Each site In  sites
    
            Dim lists As SPListCollection = site.Lists
    
            Console.WriteLine("Site: " + site.Name + "  Lists: " 
               + lists.Count.ToString())
    
        Next site
    
        Console.WriteLine("Press ENTER to continue")
        Console.ReadLine()
    
    End Sub 'Main

    C#
    static void Main(string[] args)
    {
        SPSite siteCollection = new SPSite("http://Server_Name");
        SPWebCollection sites = siteCollection.AllWebs;
    
        foreach (SPWeb site in sites)
        {
            SPListCollection lists =  site.Lists;
    
            Console.WriteLine("Site: " + site.Name + "  Lists: " 
               + lists.Count.ToString());
        }
    
        Console.WriteLine("Press ENTER to continue");
        Console.ReadLine();
    }
  9. Click Start on the Debug menu or press F5 to run the code.

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Couple things to watch out for...      David Prothero   |   Edit   |   Show History

First, didn't realize I can't run this code on my workstation. It has to run on the server that has WSS installed.

Second, when I tried to run it from the server, I was initially trying to run it directly from a fileshare on my workstation. That doesn't run the program with high enough permissions. It worked fine once I copied the EXE and DLLs to a local folder on the server.

Tags What's this?: Add a tag
Flag as ContentBug
Italic Tag      senfo   |   Edit   |   Show History
What's with the italic tag inside the URL?
Tags What's this?: Add a tag
Flag as ContentBug
Command line compile      JD Moore   |   Edit   |   Show History
OK, that's great if I want to install visual studion on the server, but what if I just want to compile this using CSC.exe? What flags do I use to compile and reference the libraries?
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker