Partager via


Exemple de code : utilisation du modèle objet Administration

Dernière modification : samedi 13 mars 2010

S’applique à : SharePoint Foundation 2010

L’application console ci-après permet de créer une liste détaillée des services, des instances de service, des applications Web, des bases de données de contenu, des collections de sites et des sites Web dans un déploiement de SharePoint Foundation.

Pour utiliser ce code, créez un projet d’application console appelé « ComponentItemization » dans Microsoft Visual Studio. Ajoutez une référence à Microsoft.SharePoint.dll, puis remplacez le contenu du fichier Program.cs par défaut par le code ci-dessous.

Pour exécuter l'exécutable, tapez ComponentItemization > output.txt à l'invite de commandes dans le répertoire où vous avez enregistré le fichier ComponentItemization.exe.

Ouvrez le fichier output.txt dans n'importe quelle visionneuse de texte pour afficher la sortie.

Notes

Cet exemple est écrit de telle sorte que vous puissiez voir les références à chaque type de composant à côté des références à ses propriétés, à ses principaux composants enfants et à son composant parent. Il évite donc délibérément d'encapsuler le code répétitif dans des méthodes distinctes.

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

Voir aussi

Référence

SPFarm

SPServer

SPService

SPWebApplication

SPDatabase

SPServiceInstance

SPSite

Concepts

Serveur et architecture du site : vue d'ensemble du modèle objet

Utilisation des objets Liste et des collections

Vue d’ensemble : utiliser le modèle objet pour personnaliser l’administration

Exemple de code : utilisation du modèle objet Administration

La hiérarchie de contenu de Microsoft SharePoint Foundation

Informations de base : entités de contenu dans Microsoft SharePoint Foundation

La hiérarchie des objets physiques de Microsoft SharePoint Foundation

Informations de base : objets physiques dans Microsoft SharePoint Foundation

Hiérarchie des services de Microsoft SharePoint Foundation

Informations de base : entités de service dans Microsoft SharePoint Foundation

Autres ressources

Administration de SharePoint Foundation