다음을 통해 공유


Code Sample: Using the Administration Object Model

Applies to: SharePoint Foundation 2010

The following console application creates an itemization of the services, service instances, Web applications, content databases, site collections, and Web sites at a SharePoint Foundation deployment.

To use this code, create a console application project named "ComponentItemization" in Microsoft Visual Studio. Add a reference to Microsoft.SharePoint.dll. Then replace the contents of the default Program.cs file with the code below.

To run the executable, enter ComponentItemization > output.txt at a command prompt in the directory where you saved ComponentItemization.exe.

Open output.txt in any text viewer to see the output.

Note

This sample is written so that you can see references to each type of component in close proximity to references to its properties, its main child components, and its parent component. For that reason, this sample deliberately avoids encapsulating repetitive code into separate methods.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace ComponentItemization
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("The Farm is: {0}\n", 
              SPFarm.Local.DisplayName);
            SPServiceCollection myServices = SPFarm.Local.Services;

            Console.WriteLine("\tThe services in the farm:\n");
            
            // Itemize the Windows Services
            Console.WriteLine("\n\n\t\tThe Windows Services:");
            foreach (SPService sps in myServices)
            {
                if (sps is SPWindowsService)
                {
                    Console.WriteLine("\n\t\t\tService Type Name: {0}", 
                      sps.TypeName);
                    Console.WriteLine("\t\t\tService Name: {0}\n", 
                      sps.Name);

                    Console.WriteLine("\t\t\tThe instances of this service:\n");
                    Int16 serviceInstanceNumber = 1;
                    SPServiceInstanceDependencyCollection 
                      winServiceIntCol = sps.Instances;
                    foreach (SPServiceInstance winSerInt 
                      in winServiceIntCol)
                    {
                        Console.WriteLine("\t\t\t\tInstance {0}:", 
                          serviceInstanceNumber);
                        Console.WriteLine("\t\t\t\tInstance 
                          DisplayName: {0}", winSerInt.DisplayName);
                        Console.WriteLine("\t\t\t\tInstance Name: {0}", 
                          winSerInt.Name);
                        Console.WriteLine("\t\t\t\tInstance Hosting Server: " 
                          + GetInstanceHostingServerName(winSerInt.Server) 
                          + "\n");
                        serviceInstanceNumber++;
                    }
                }
            }

            // Itemize the Web Services
            Console.WriteLine("\n\n\t\tThe Web Services:");
            foreach (SPService sps in myServices)
            {
                if (sps is SPWebService)
                {
                    Console.WriteLine("\n\t\t\tService Type name: {0}", 
                      sps.TypeName);
                    Console.WriteLine("\t\t\tService Name: {0}\n", 
                      sps.Name);

                    Console.WriteLine("\t\t\tThe instances of this service:\n");
                    Int16 serviceInstanceNumber = 1;
                    SPServiceInstanceDependencyCollection 
                      webServiceIntCol = sps.Instances;
                    foreach (SPServiceInstance webSerInt 
                      in webServiceIntCol)
                    {
                        Console.WriteLine("\t\t\t\tInstance {0}:", 
                          serviceInstanceNumber);
                        Console.WriteLine("\t\t\t\tInstance DisplayName: {0}", 
                          webSerInt.DisplayName);
                        Console.WriteLine("\t\t\t\tInstance Name: {0}", 
                          webSerInt.Name);
                        Console.WriteLine("\t\t\t\tInstance Hosting Server: " 
                          + GetInstanceHostingServerName(webSerInt.Server) 
                          + "\n");
                        serviceInstanceNumber++;
                    }

                    Console.WriteLine("\n\t\t\tThe Web applications in this Web service:\n");
                    Int32 webAppNumber = 1;
                    SPWebService spws = (SPWebService)sps;
                    foreach (SPWebApplication spwebapp 
                      in spws.WebApplications)
                    {
                        Console.WriteLine("\t\t\t\tWeb Application {0}", 
                          webAppNumber);
                        Console.WriteLine("\t\t\t\tApplication Name: {0}", 
                          spwebapp.Name);
                        Console.WriteLine("\t\t\t\tApplication Display Name: {0}\n", 
                          spwebapp.DisplayName);
                        webAppNumber++;

                        Console.WriteLine("\n\t\t\t\tThe content databases in this Web application:\n");
                        Int32 contentDBNumber = 1;
                        foreach (SPContentDatabase db 
                          in spwebapp.ContentDatabases)
                        {
                            Console.WriteLine("\n\t\t\t\t\tContent Database {0}", 
                              contentDBNumber);
                            Console.WriteLine("\t\t\t\t\tDatabase Name: {0}", 
                              db.DisplayName);
                            contentDBNumber++;
                            
                            Console.WriteLine("\n\t\t\t\t\t\tThe site collections in this database:\n");
                            Int32 siteColNumber = 1;
                            
                            foreach (SPSite site in db.Sites)
                            {
                                Console.WriteLine("\n\t\t\t\t\t\t\tSite Collection {0}", 
                                  siteColNumber);
                                Console.WriteLine("\t\t\t\t\t\t\tSite Collection RootWeb: {0}", 
                                  site.RootWeb);
                                Console.WriteLine("\t\t\t\t\t\t\tSite Collection Url: {0}", 
                                  site.Url);
                                siteColNumber++;

                                Console.WriteLine("\n\t\t\t\t\t\t\tThe Web sites in this site collection:");
                                SPWebCollection webs = site.AllWebs;
                                Int64 webSiteNumber = 1;
                                foreach (SPWeb web in webs)
                                {
                                    Console.WriteLine("\t\t\t\t\t\t\t\tWeb site {0}: {1}", 
                                      webSiteNumber, web.Name);
                                    webSiteNumber++;
                                }
                            }
                        }
                    }
                }
            }

            // Itemize the other services
            Console.WriteLine("\n\n\t\tOther Services:");
            foreach (SPService sps in myServices)
            {
                if (!(sps is SPWebService) 
                  && !(sps is SPWindowsService))
                {
                    Console.WriteLine("\n\t\t\tService Type Name: {0}", 
                      sps.TypeName);
                    Console.WriteLine("\t\t\tService Name: {0}", 
                      sps.Name);

                    Console.WriteLine("\t\t\tThe instances of this service:\n");
                    Int16 serviceInstanceNumber = 1;
                    SPServiceInstanceDependencyCollection 
                      otherServiceIntCol = sps.Instances;
                    foreach (SPServiceInstance otherSerInt 
                      in otherServiceIntCol)
                    {
                        Console.WriteLine("\t\t\t\tInstance {0}:", 
                          serviceInstanceNumber);
                        Console.WriteLine("\t\t\t\tInstance DisplayName: {0}", 
                          otherSerInt.DisplayName);
                        Console.WriteLine("\t\t\t\tInstance Name: {0}", 
                          otherSerInt.Name);
                        Console.WriteLine("\t\t\t\tInstance Hosting Server: " 
                          + GetInstanceHostingServerName(otherSerInt.Server) 
                          + "\n");
                        serviceInstanceNumber++;
                    }
                }
            }
            
            // To send output to the console, uncomment the following lines:
            // Console.WriteLine("Press Enter to continue.");
            // Console.ReadLine();

        }//end Main

        private static 
          String GetInstanceHostingServerName(SPServer instanceHostingServer)
        {
            String full = instanceHostingServer.ToString();
            String concise = full.Substring(full.IndexOf("=") + 1, 
              full.IndexOf("Parent") - (full.IndexOf("=") + 1));
            return concise;
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Administration

Namespace ComponentItemization
    Module Module1
        Sub Main(ByVal args() As String)
            Console.WriteLine("The Farm is: {0}" & vbLf, SPFarm.Local.DisplayName)
            Dim myServices As SPServiceCollection = SPFarm.Local.Services

            Console.WriteLine(vbTab & "The services in the farm:" & vbLf)

            ' Itemize the Windows Services
            Console.WriteLine(vbLf & vbLf & vbTab & vbTab & "The Windows Services:")
            For Each sps As SPService In myServices
                If TypeOf sps Is SPWindowsService Then
                    Console.WriteLine(vbLf & vbTab & vbTab & vbTab & "Service Type Name: {0}", sps.TypeName)
                    Console.WriteLine(vbTab & vbTab & vbTab & "Service Name: {0}" & vbLf, sps.Name)

                    Console.WriteLine(vbTab & vbTab & vbTab & "The instances of this service:" & vbLf)
                    Dim serviceInstanceNumber As Int16 = 1
                    Dim winServiceIntCol As SPServiceInstanceDependencyCollection = sps.Instances
                    For Each winSerInt As SPServiceInstance In winServiceIntCol
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance {0}:", serviceInstanceNumber)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance DisplayName: {0}", winSerInt.DisplayName)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance Name: {0}", winSerInt.Name)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance Hosting Server: " & GetInstanceHostingServerName(winSerInt.Server) & vbLf)
                        serviceInstanceNumber += 1
                    Next
                End If
            Next

            ' Itemize the Web Services
            Console.WriteLine(vbLf & vbLf & vbTab & vbTab & "The Web Services:")
            For Each sps As SPService In myServices
                If TypeOf sps Is SPWebService Then
                    Console.WriteLine(vbLf & vbTab & vbTab & vbTab & "Service Type name: {0}", sps.TypeName)
                    Console.WriteLine(vbTab & vbTab & vbTab & "Service Name: {0}" & vbLf, sps.Name)

                    Console.WriteLine(vbTab & vbTab & vbTab & "The instances of this service:" & vbLf)
                    Dim serviceInstanceNumber As Int16 = 1
                    Dim webServiceIntCol As SPServiceInstanceDependencyCollection = sps.Instances
                    For Each webSerInt As SPServiceInstance In webServiceIntCol
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance {0}:", serviceInstanceNumber)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance DisplayName: {0}", webSerInt.DisplayName)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance Name: {0}", webSerInt.Name)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance Hosting Server: " & GetInstanceHostingServerName(webSerInt.Server) & vbLf)
                        serviceInstanceNumber += 1
                    Next

                    Console.WriteLine(vbLf & vbTab & vbTab & vbTab & "The Web applications in this Web service:" & vbLf)
                    Dim webAppNumber As Int32 = 1
                    Dim spws As SPWebService = CType(sps, SPWebService)
                    For Each spwebapp As SPWebApplication In spws.WebApplications
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Web Application {0}", webAppNumber)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Application Name: {0}", spwebapp.Name)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Application Display Name: {0}" & vbLf, spwebapp.DisplayName)
                        webAppNumber += 1

                        Console.WriteLine(vbLf & vbTab & vbTab & vbTab & vbTab & "The content databases in this Web application:" & vbLf)
                        Dim contentDBNumber As Int32 = 1
                        For Each db As SPContentDatabase In spwebapp.ContentDatabases
                            Console.WriteLine(vbLf & vbTab & vbTab & vbTab & vbTab & vbTab & "Content Database {0}", contentDBNumber)
                            Console.WriteLine(vbTab & vbTab & vbTab & vbTab & vbTab & "Database Name: {0}", db.DisplayName)
                            contentDBNumber += 1

                            Console.WriteLine(vbLf & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "The site collections in this database:" & vbLf)
                            Dim siteColNumber As Int32 = 1

                            For Each site As SPSite In db.Sites
                                Console.WriteLine(vbLf & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "Site Collection {0}", siteColNumber)
                                Console.WriteLine(vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "Site Collection RootWeb: {0}", site.RootWeb)
                                Console.WriteLine(vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "Site Collection Url: {0}", site.Url)
                                siteColNumber += 1

                                Console.WriteLine(vbLf & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "The Web sites in this site collection:")
                                Dim webs As SPWebCollection = site.AllWebs
                                Dim webSiteNumber As Int64 = 1
                                For Each web As SPWeb In webs
                                    Console.WriteLine(vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & vbTab & "Web site {0}: {1}", webSiteNumber, web.Name)
                                    webSiteNumber += 1
                                Next
                            Next
                        Next
                    Next
                End If
            Next

            ' Itemize the other services
            Console.WriteLine(vbLf & vbLf & vbTab & vbTab & "Other Services:")
            For Each sps As SPService In myServices
                If Not (TypeOf sps Is SPWebService) AndAlso Not (TypeOf sps Is SPWindowsService) Then
                    Console.WriteLine(vbLf & vbTab & vbTab & vbTab & "Service Type Name: {0}", sps.TypeName)
                    Console.WriteLine(vbTab & vbTab & vbTab & "Service Name: {0}", sps.Name)

                    Console.WriteLine(vbTab & vbTab & vbTab & "The instances of this service:" & vbLf)
                    Dim serviceInstanceNumber As Int16 = 1
                    Dim otherServiceIntCol As SPServiceInstanceDependencyCollection = sps.Instances
                    For Each otherSerInt As SPServiceInstance In otherServiceIntCol
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance {0}:", serviceInstanceNumber)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance DisplayName: {0}", otherSerInt.DisplayName)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance Name: {0}", otherSerInt.Name)
                        Console.WriteLine(vbTab & vbTab & vbTab & vbTab & "Instance Hosting Server: " & GetInstanceHostingServerName(otherSerInt.Server) & vbLf)
                        serviceInstanceNumber += 1
                    Next
                End If
            Next

            ' To send output to the console, uncomment the following lines:
            ' Console.WriteLine("Press Enter to continue.");
            ' Console.ReadLine();

        End Sub 'end Main

        Private Function GetInstanceHostingServerName(ByVal instanceHostingServer As SPServer) As String
            Dim full As String = instanceHostingServer.ToString()
            Dim concise As String = full.Substring(full.IndexOf("=") + 1, full.IndexOf("Parent") - (full.IndexOf("=") + 1))
            Return concise
        End Function
    End Module
End Namespace

See Also

Reference

SPFarm

SPServer

SPService

SPWebApplication

SPDatabase

SPServiceInstance

SPSite

Concepts

Server and Site Architecture: Object Model Overview

Working with List Objects and Collections

Overview: Using the Object Model to Customize Administration

Code Sample: Using the Administration Object Model

Content Hierarchy of SharePoint Foundation

Background: Content Entities in Microsoft SharePoint Foundation

Physical Objects Hierarchy of Microsoft SharePoint Foundation

Background: Physical Objects in Microsoft SharePoint Foundation

Services Hierarchy of Microsoft SharePoint Foundation

Background: Service Entities in Microsoft SharePoint Foundation

Other Resources

SharePoint Foundation Administration