如何:扩展 STSADM 实用工具
上次修改时间: 2010年5月1日
适用范围: SharePoint Foundation 2010
警告 |
---|
扩展 STSADM.EXE 的方法已被弃用,并且在未来的 Microsoft SharePoint Foundation 2010 发行版本中将不会提供相关支持。提供此主题完全是为了帮助对现有的 STSADM.EXE 扩展进行疑难解答。您不应创建新的扩展,而应考虑扩展 Windows PowerShell。请参阅 SharePoint Management Shell 中的 Windows PowerShell。 |
利用 STSADM.EXE 实用工具,可以执行无法在管理中心应用程序中完成的很多 Windows SharePoint Services 管理操作。有关详细信息,请参阅 Microsoft TechNet 中的文章 Stsadm.exe 命令行工具 (Office SharePoint Server)。您可以使用 Windows SharePoint Services 3.0 扩展 STSADM 实用工具的功能,方法是通过使用任意 .NET 语言的简单项目来添加您自己的操作和命令行参数。
创建此类项目需要执行两个主要任务。
创建一个可实现 ISPStsadmCommand 接口的类。
注册该类及其程序集以向 STSADM 通知您的相关扩展。
创建一个可实现 ISPStsadmCommand 的类
在 Visual Studio 中启动一个类库项目。
为 Microsoft.SharePoint 和 Microsoft.SharePoint.StsAdmin 添加 using 语句。
使用一个遵循 CompanyName.TechnologyName.Feature.SubFeature 模式的命名空间,如 AjaxInc.SharePoint.StsAdmin.CustomCommands(请参阅Names of Namespaces)。
使用一个表示您要创建的新 STSADM 操作的公约数的类名称;例如,"SortCommands"。
该类应继承 ISPStsadmCommand;其声明如下所示。
公共类 SortCommands:ISPStsAdminCommand
编写 GetHelpMessage 方法的实现。请参阅下面的示例。
编写 Run 方法的实现。请参阅下面的示例。
编译项目,将命名空间名称用作程序集的名称。
将程序集部署到全局程序集缓存;例如 C:\Windows\Assembly。
注册新的类和程序集
创建一个名为 stsadmcommands.uniqueID.xml 的文本文件 (UTF-8),其中 uniqueID 为贵公司的名称或其他某些 ID,这些 ID 可确保在可能部署了 STSADM 扩展的任何服务器上具有唯一性。XML 声明仅应读取 <?xml version="1.0" encoding="utf-8" ?>。顶级元素为 <commands></commands>。
对于您所创建的每个自定义 STSADM 操作(即 GetHelpMessage 和 Run 的 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>
用适当的值替换 command_name、fully_qualified_class_name 和 assembly_name(不要在程序集名称中包括".dll"扩展名)。
使用通过以下步骤获得的程序集的公钥标记替换 value。
在全局程序集缓存中右键单击相应的程序集,并选择"属性"。
在"常规"选项卡上,复制"公钥标记"值。
将其粘贴为 PublicKeyToken 的值。
将 stsadmcommands.uniqueID.xml 文件复制到 C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG。
示例
下面的示例首先演示 *.cs 文件,然后演示将在网站上列出功能的自定义 STSADM 操作(称为 enumfeatures)的 stsadmcommands.uniqueID.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>