コード例 : 管理オブジェクト モデルを使用する

最終更新日: 2010年3月13日

適用対象: SharePoint Foundation 2010

次のコンソール アプリケーションでは、SharePoint Foundation 展開のサービス、サービス インスタンス、Web アプリケーション、コンテンツ データベース、サイト コレクション、および Web サイトの明細が作成されます。

このコードを使用するには、Microsoft Visual Studio に "ComponentItemization" という名前のコンソール アプリケーション プロジェクトを作成し、Microsoft.SharePoint.dll への参照を追加します。次に、既定の Program.cs ファイルのコンテンツを次のコードに置き換えます。

実行可能ファイルを実行するには、ComponentItemization.exe を保存したディレクトリで、コマンド プロンプトに「ComponentItemization > output.txt」と入力します。

出力を確認するには、任意のテキスト ビューアーで output.txt を開きます。

注意

次のサンプルは、各種類のコンポーネントへの参照をそのプロパティ、メインの子コンポーネント、および親コンポーネントへの参照と共に確認することを目的として記述されています。このため、このサンプルの繰り返しコードは、別のメソッドに意図的にカプセル化されていません。

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

関連項目

参照

SPFarm

SPServer

SPService

SPWebApplication

SPDatabase

SPServiceInstance

SPSite

概念

サーバーとサイトのアーキテクチャ : オブジェクト モデルの概要

リストのオブジェクトとコレクションを操作する

概要 : オブジェクト モデルを使用して管理をカスタマイズする

コード例 : 管理オブジェクト モデルを使用する

Microsoft SharePoint Foundation のコンテンツ階層

[バックグラウンド] Microsoft SharePoint Foundation のコンテンツ エンティティ

Microsoft SharePoint Foundation の物理オブジェクト階層

[バックグラウンド] Microsoft SharePoint Foundation の物理オブジェクト

Microsoft SharePoint Foundation のサービス階層

バックグラウンド: Microsoft SharePoint Foundation のサービス エンティティ

その他の技術情報

SharePoint Foundation の管理