Partager via


Creating a Console Application

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

Note  For users to run a console application in the context of Microsoft Windows SharePoint Services 2.0, they must be administrators on the computer where the script is executed.

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

  • In the New Project dialog box, select Visual Basic Projects or Visual C# Projects in the Project Types box, depending on which language you prefer.

  • In the Templates box, select Console Application.

  • In the Location box, type the path to where to create the application, 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 Windows SharePoint Services in the list of components, click Select, and then click OK.

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

    [Visual Basic .NET]
    
    Imports Microsoft.SharePoint
    
    [C#]
    
    using Microsoft.SharePoint;
    
  • Add the following code to the Main method in the .vb or .cs file.

    [Visual Basic .NET]
    
    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();
    }
    
  • Click Start on the Debug menu or press F5 to run the code.