次の方法で共有


STSADM ユーティリティを拡張する

最終更新日: 2010年5月1日

適用対象: SharePoint Foundation 2010

注意注意

STSADM.EXE を拡張することは推奨されておらず、Microsoft SharePoint Foundation 2010 の将来のリリースではサポートされなくなる予定です。このトピックは、STSADM.EXE の既存の拡張機能のトラブルシューティングを容易にすることのみを目的にしています。新たに拡張機能を作成するのは避け、Windows PowerShell の拡張を検討してください。「SharePoint 管理シェルにおける Windows PowerShell」を参照してください。

STSADM.EXE ユーティリティを使用して、全体管理アプリケーションではできない Windows SharePoint Services のさまざまな管理操作を行うことができます。詳細については、Microsoft TechNet の記事「Stsadm コマンド ライン ツール (Office SharePoint Server)」を参照してください。Windows SharePoint Services 3.0 では、任意の .NET 言語を使用した単純なプロジェクトに独自の操作やコマンド ライン パラメーターを追加することにより、STSADM ユーティリティの機能を拡張できます。

このようなプロジェクトの作成には、主に 2 つの作業が必要です。

  1. ISPStsadmCommand インターフェイスを実装するクラスを作成します。

  2. クラスとそのアセンブリを登録して、拡張情報を STSADM に通知します。

ISPStsadmCommand を実装するクラスを作成する

  1. クラス ライブラリ プロジェクトを Visual Studio で起動します。

  2. Microsoft.SharePoint および Microsoft.SharePoint.StsAdmin の using ステートメントを追加します。

  3. CompanyName.TechnologyName.Feature.SubFeature のパターンにしたがって名前空間を使用します。たとえば、AjaxInc.SharePoint.StsAdmin.CustomCommands のようにします (Names of Namespaces を参照)。

  4. 作成する新しい STSADM 操作の共通項を表すクラス名 (例 : "SortCommands") を使用します。

  5. クラスは、次のような宣言で ISPStsadmCommand を継承する必要があります。

    public class SortCommands : ISPStsAdminCommand

  6. GetHelpMessage メソッドの実装を記述します。後述の例を参照してください。

  7. Run メソッドの実装を記述します。後述の例を参照してください。

  8. アセンブリ名に名前空間名を指定してプロジェクトをコンパイルします。

  9. アセンブリをグローバル アセンブリ キャッシュ (例 : C:\Windows\Assembly) に展開します。

新しいクラスおよびアセンブリを登録する

  1. stsadmcommands.uniqueID.xml という名前のテキスト ファイル (UTF-8) を作成します。uniqueID は、STSADM の拡張を展開するサーバーを一意に識別するための会社名などの ID です。XML 宣言は単純に <?xml version="1.0" encoding="utf-8" ?> とします。最上位の要素は <commands></commands> です。

  2. 作成した各カスタム STSADM 操作に対して (GetHelpMessage および Run の command パラメーターの有効な各値に対して)、次の構文で stsadmcommands ファイルの <commands> 要素の内側に <command/> 要素を追加します (次の例を参照)。必要に応じてバージョンおよびカルチャの値を変更します。

    <commands>
        <command 
            name="command_name" 
            class="fully_qualified_class_name, assembly_name, 
            Version=1.0.0.0, 
            Culture=neutral, 
            PublicKeyToken=value"/>
        <!-- other command elements, if any -->
    </commands>
    
  3. command_name、fully_qualified_class_name、および assembly_name を適切な値に変更します (アセンブリ名に .dll 拡張子を付けないでください)。

  4. value を、以下の手順で取得したアセンブリの公開キー トークンに変更します。

    1. グローバル アセンブリ キャッシュ内のアセンブリを右クリックし、[プロパティ] を選択します。

    2. [全般] タブで [公開キー トークン] の値をコピーします。

    3. この値を PublicKeyToken の値として貼り付けます。

  5. stsadmcommands.uniqueID.xml ファイルを C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG にコピーします。

次の例は *.cs ファイルを示しています。また、その下に、サイトの機能を一覧表示するカスタム STSADM 操作用の enumfeatures と呼ばれる stsadmcommands.一意の ID.xml ファイルを示しています。

using System;
using System.Collections.Specialized;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.StsAdmin;

namespace MS.Samples.SharePoint
{
    public class SimpleCommandHandler : ISPStsadmCommand
    {
        public string GetHelpMessage(string command)
        {
            return "-url <full url to a site in SharePoint>";
        }

        public int Run(string command, StringDictionary keyValues, out string output)
        {
            command = command.ToLowerInvariant();

            switch (command)
            {
                case "enumfeatures":
                    return this.EnumerateFeatures(keyValues, out output);

                default:
                    throw new InvalidOperationException();
            }
        }

        private int EnumerateFeatures(StringDictionary keyValues, out string output)
        {
            if (!keyValues.ContainsKey("url"))
            {
                throw new InvalidOperationException("The url parameter was not specified.");
            }

            String url = keyValues["url"];

            SPFeatureCollection features = null;
            SPWeb web = null;

            try
            {
                SPSite site = new SPSite(url);

                web = site.OpenWeb();

                features = web.Features;
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("Error retrieving url '" + url + "'.  Please check the format of your url, and ensure that the site exists.  Details: " + e.Message);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Features at '" + web.Url + "':\n");

            foreach (SPFeature feature in features)
            {
                sb.AppendLine(feature.Definition.DisplayName + " (" + feature.DefinitionId + ")");
            }
            
            output = sb.ToString();

            return 0;
        }
    }
}
Imports System
Imports System.Collections.Specialized
Imports System.Text
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.StsAdmin

Namespace MS.Samples.SharePoint
    Public Class SimpleCommandHandler
        Inherits ISPStsadmCommand
        Public Function GetHelpMessage(ByVal command As String) As String
            Return "-url <full url to a site in SharePoint>"
        End Function

        Public Function Run(ByVal command As String, ByVal keyValues As StringDictionary, <System.Runtime.InteropServices.Out()> ByRef output As String) As Integer
            command = command.ToLowerInvariant()

            Select Case command
                Case "enumfeatures"
                    Return Me.EnumerateFeatures(keyValues, output)

                Case Else
                    Throw New InvalidOperationException()
            End Select
        End Function

        Private Function EnumerateFeatures(ByVal keyValues As StringDictionary, <System.Runtime.InteropServices.Out()> ByRef output As String) As Integer
            If Not keyValues.ContainsKey("url") Then
                Throw New InvalidOperationException("The url parameter was not specified.")
            End If

            Dim url As String = keyValues("url")

            Dim features As SPFeatureCollection = Nothing
            Dim web As SPWeb = Nothing

            Try
                Dim site As New SPSite(url)

                web = site.OpenWeb()

                features = web.Features
            Catch e As Exception
                Throw New InvalidOperationException("Error retrieving url '" & url & "'.  Please check the format of your url, and ensure that the site exists.  Details: " & e.Message)
            End Try

            Dim sb As New StringBuilder()

            sb.AppendLine("Features at '" & web.Url & "':" & vbLf)

            For Each feature As SPFeature In features
                sb.AppendLine(feature.Definition.DisplayName & " (" & feature.DefinitionId & ")")
            Next feature

            output = sb.ToString()

            Return 0
        End Function
    End Class
End Namespace
<?xml version="1.0" encoding="utf-8" ?>

<commands>
    <command 
        name="enumfeatures" 
        class="MS.Samples.SharePoint.SimpleCommandHandler, MS.Samples.SharePoint.CustomStsAdmCommand, 
        Version=1.0.0.0, 
        Culture=neutral, 
        PublicKeyToken=4da7a49e92ae373c"/>
</commands>

関連項目

参照

ISPStsadmCommand

GetHelpMessage

Run

その他の技術情報

Names of Namespaces

Stsadm コマンド ライン ツール (Office SharePoint Server)