Freigeben über


Codebeispiel: Verwenden des Objektmodells für die Administration

Letzte Änderung: Samstag, 13. März 2010

Gilt für: SharePoint Foundation 2010

Die folgende Konsolenanwendung erstellt bei einer SharePoint Foundation-Bereitstellung eine Einzelauflistung von Diensten, Dienstinstanzen, Webanwendungen, Inhaltsdatenbanken, Websitesammlungen und Websites.

Um diesen Code zu verwenden, erstellen Sie in Microsoft Visual Studio ein Konsolenanwendungsprojekt mit dem Namen ComponentItemization. Fügen Sie einen Verweis auf Microsoft.SharePoint.dll hinzu. Ersetzen Sie dann den Inhalt der Standarddatei Program.cs durch den folgenden Code.

Geben Sie zum Ausführen der ausführbaren Datei an einer Eingabeaufforderung in dem Verzeichnis, in dem Sie ComponentItemization.exe gespeichert haben, ComponentItemization > output.txt ein.

Öffnen Sie output.txt in einem beliebigen Textviewer, um die Ausgabe anzuzeigen.

HinweisHinweis

Dieses Beispiel wurde so geschrieben, dass Verweise auf die einzelnen Typen von Komponenten direkt bei Verweisen auf die zugehörigen Eigenschaften, die wichtigsten untergeordneten Komponenten und die übergeordnete Komponente angeordnet sind. Aus diesem Grund wird in diesem Beispiel repetitiver Code bewusst nicht in eigenen Methoden gekapselt.

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

Siehe auch

Referenz

SPFarm

SPServer

SPService

SPWebApplication

SPDatabase

SPServiceInstance

SPSite

Konzepte

Server- und Websitearchitektur: Übersicht über das Objektmodell

Arbeiten mit Listenobjekten und Auflistungen

Übersicht: Verwenden des Objektmodells zum Anpassen der Verwaltung

Codebeispiel: Verwenden des Objektmodells für die Administration

Die Inhaltshierarchie von Microsoft SharePoint Foundation

Hintergrund: Inhaltsentitäten in Microsoft SharePoint Foundation

Die Hierarchie der physikalischen Objekte von Microsoft SharePoint Foundation

Hintergrund: Physikalische Objekte in Microsoft SharePoint Foundation

Dienstehierarchie von Microsoft SharePoint Foundation

Hintergrund: Dienstentitäten in Microsoft SharePoint Foundation

Weitere Ressourcen

SharePoint Foundation-Administration