演练:替换服务器功能区中的网站集按钮

上次修改时间: 2011年2月4日

适用范围: SharePoint Foundation 2010

本文内容
创建 SharePoint 项目
添加新功能
定义自定义操作
部署自定义项

本主题介绍 Microsoft SharePoint Foundation 中服务器功能区上的按钮的网站集级别替换。可以将自定义操作与 ScriptLink 位置一起使用以在所有页面上添加 ECMAScript(JavaScript、JScript)。

先决条件

Microsoft SharePoint Foundation 2010

Microsoft Visual Studio 2010 中的 SharePoint 开发工具

创建 SharePoint 项目

若要自定义功能区,请首先创建一个空白 SharePoint 项目。

创建 SharePoint 项目

  1. 启动 Microsoft Visual Studio 2010。

  2. 在"文件"菜单上,指向"新建",然后单击"项目"。

  3. 在"项目类型"中,选择"C#",然后选择"空白 SharePoint 项目"。

  4. 键入 ReplaceARibbonButtonUsingScriptLink 作为项目名称,然后单击"确定"。

  5. 在"SharePoint 自定义向导"中,选择"部署为场解决方案",然后单击"完成"。

添加新功能

可以使用功能自定义功能区。执行以下步骤将向您的解决方案中添加新功能。

添加新功能

  1. 在"解决方案资源管理器"中,右键单击"功能",然后选择"添加功能"。

  2. 将功能的"标题"更改为 Replace a Ribbon Button Using ScriptLink。

  3. 在"解决方案资源管理器"中,右键单击"Feature1",然后选择"重命名"。键入 ReplaceARibbonButtonUsingScriptLink 作为新名称。

  4. 在"解决方案资源管理器"中,右键单击"ReplaceARibbonButtonUsingScriptLink"项目,指向"添加",然后选择"新建项"。

  5. 在"添加新项"对话框中,选择"空元素"模板。键入 ReplaceARibbonButtonUsingScriptLink 作为名称。

定义自定义操作

可以使用 CommandUIDefinition 元素的 Location 属性替换功能区按钮。默认服务器功能区自定义位置中列出了功能区按钮的默认值。有关功能区 XML 的详细说明,请参阅服务器功能区 XML

定义自定义操作

  1. 打开 Elements.xml 文件。

  2. 将以下 XML 粘贴到 Elements.xml 文件中。此 XML 将替换文档库的"库"选项卡上"连接和导出"组中的"连接到 Outlook"按钮。

    重要注释重要信息

    必须将 Image32by32 和 Image16by16 属性替换为有效的图像 URL。

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
      <CustomAction Id="Ribbon.Library.Actions.ConnectToClient"
        Location="CommandUI.Ribbon"
        RegistrationId="101"
        RegistrationType="List"
        Title="Custom ECMAScript Button">
        <CommandUIExtension>
          <CommandUIDefinitions>
            <CommandUIDefinition
              Location="Ribbon.Library.Actions.ConnectToClient">
              <Button Id="Ribbon.Library.Actions.ConnectToClient"
                Command="CustomECMAScriptCommand"
                Image16by16="Insert an image URL here."
                Image32by32="Insert an image URL here."
                LabelText="Hello World"
                TemplateAlias="o1" />
            </CommandUIDefinition>
          </CommandUIDefinitions>
          <CommandUIHandlers>
            <CommandUIHandler
              Command="CustomECMAScriptCommand"
              CommandAction="javascript:HelloWorld();" />
          </CommandUIHandlers>
        </CommandUIExtension>
      </CustomAction>
      <CustomAction Id="Ribbon.Library.Actions.NewButton.Script"
        Location="ScriptLink"
        ScriptBlock="
        function HelloWorld()
        {
          alert('Hello, world!');
        }" />
    </Elements>
    

部署自定义项

由于该项目是作为服务器场解决方案创建的,因此它将自动部署并且 Internet Information Services (IIS) 将重置。

部署自定义项

  1. 按 F5。Visual Studio 2010 中的 SharePoint 开发工具将自动构建和部署功能。

  2. 导航到网站或子网站中的文档库。

  3. 单击"库"选项卡,查看"连接和导出"组,注意其中的"连接到 Outlook"按钮已不存在。

请参阅

概念

服务器功能区 XML

服务器功能区的声明性自定义项

EcmaScript 和服务器功能区

服务器功能区的命令性自定义项