如何:扩展 STSADM 实用工具【转载】

本文转载自微软官方网站: http://msdn.microsoft.com/zh-cn/library/bb417382.aspx

利用 STSADM.EXE 实用工具,可以执行无法在管理中心应用程序中完成的很多 Windows SharePoint Services 管理操作。有关详细信息,请参阅 Microsoft TechNet 中的文章 Stsadm.exe command-line tool (Office SharePoint Server)。您可以使用 Windows SharePoint Services 3.0 扩展 STSADM 实用工具的功能,方法是通过使用任意 .NET 语言的简单项目来添加您自己的操作和命令行参数。

创建此类项目需要执行两个主要任务。

  1. 创建一个可实现 ISPStsadmCommand 接口的类。

  2. 注册该类及其程序集以向 STSADM 通知您的相关扩展。

创建一个可实现 ISPStsadmCommand 的类
  1. 在 Visual Studio 中启动一个类库项目。

  2. Microsoft.SharePointMicrosoft.SharePoint.StsAdmin 添加 using 语句。

  3. 使用一个遵循 CompanyName.TechnologyName.Feature.SubFeature 模式的命名空间,如 AjaxInc.SharePoint.StsAdmin.CustomCommands(请参阅Names of Namespaces)。

  4. 使用一个表示您要创建的新 STSADM 操作的公约数的类名称;例如,“SortCommands”。

  5. 该类应继承 ISPStsadmCommand;其声明如下所示。

    公共类 SortCommands:ISPStsAdminCommand

  6. 编写 GetHelpMessage 方法的实现。请参阅下面的示例。

  7. 编写 Run 方法的实现。请参阅下面的示例。

  8. 编译项目,将命名空间名称用作程序集的名称。

  9. 将程序集部署到全局程序集缓存;例如 C:\Windows\Assembly。

注册新的类和程序集
  1. 创建一个名为 stsadmcommands.uniqueID.xml 的文本文件 (UTF-8),其中 uniqueID 为贵公司的名称或其他某些 ID,这些 ID 可确保在可能部署了 STSADM 扩展的任何服务器上具有唯一性。XML 声明仅应读取 <?xml version="1.0" encoding="utf-8" ?>。顶级元素为 <commands></commands>

  2. 对于您所创建的每个自定义 STSADM 操作(即 GetHelpMessageRun 的 command 参数的每个可能值),请使用以下语法将 <command/> 元素(位于 <commands> 元素内)添加到 stsadmcommands 文件中(请参阅以下示例)。根据需要更改版本和区域性值。

    复制代码

    <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_namefully_qualified_class_nameassembly_name(不要在程序集名称中包括“.dll”扩展名)。

  4. 使用通过以下步骤获得的程序集的公钥标记替换 value

    1. 在全局程序集缓存中右键单击相应的程序集,并选择“属性”

    2. 在“常规”选项卡上,复制“公钥标记”值。

    3. 将其粘贴为 PublicKeyToken 的值。

  5. 将 stsadmcommands.uniqueID.xml 文件复制到 C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG

 

下面是一个范例

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;
        }
    }
}

配置文件

<?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>
原文地址:https://www.cnblogs.com/chenxizhang/p/1367444.html