如何:使用自定义操作修改用户界面

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

适用范围: SharePoint Foundation 2010

本文内容
创建 SharePoint 项目
向服务器功能区添加按钮
向"网站操作"菜单添加项目
添加编辑控制块菜单项

使用功能可以轻松地向 中的菜单和服务器功能区添加操作。下面的示例演示如何通过功能向各个菜单添加操作。每个示例均使用在 Microsoft Visual Studio 2010 中的 SharePoint 开发工具中创建的同一项目。

创建 SharePoint 项目

  1. 启动 Visual Studio 2010 中的 SharePoint 开发工具。

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

  3. 在"项目类型"中的"Visual Basic"或"C#"下,选择"空白 SharePoint 项目"。

  4. 键入 UserInterfaceActions 作为项目名称。单击"确定"。

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

  6. 在"解决方案资源管理器"中右键单击"UserInterfaceActions"项目,并依次选择"添加"和"新项"。

  7. 在"添加新项目"对话框中,选择"空元素"模板。输入 UserInterfaceActions 作为"名称"。

  8. 打开 Elements.xml 文件。

向服务器功能区添加按钮

用于与 SharePoint Foundation 交互的主要命令集位于功能区中。功能区可以使用功能自定义操作和功能区 XML 进行自定义。下列步骤演示如何使用自定义操作和功能区 XML 修改功能区。在每个示例中,CustomAction 元素的 Location 属性是不同的,以定义将显示自定义项的表单。CommandUIDefinition 元素的 Location 属性定义自定义项将显示在功能区的什么位置。有关服务器功能区的详细信息,请参阅 SharePoint Foundation 中的服务器功能区

向列表表单添加按钮

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="ListViewCustomization" Location="CommandUI.Ribbon.ListView" RegistrationId="101" RegistrationType="List" Title="List View Ribbon Customization">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Share.Controls._children">
          <Button Id="ListViewButton" Command="ListViewButtonCommand" Description="Go to Settings" LabelText="Site Settings" TemplateAlias="o2" Sequence="93"/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="ListViewButtonCommand" CommandAction="/_layouts/settings.aspx" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>

向编辑表单添加按钮

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="EditFormCustomization" Location="CommandUI.Ribbon.EditForm" RegistrationId="101" RegistrationType="List" Title="Edit Form Ribbon Customization">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.DocLibListForm.Edit.Actions.Controls._children">
          <Button Id="EditFormButtonTest" Command="EditFormButtonCommand" Description="Go to Settings" LabelText="Site Settings" TemplateAlias="o2" Sequence="91"/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="EditFormButtonCommand" CommandAction="/_layouts/settings.aspx" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>

向特定内容类型的服务器功能区添加按钮

功能区按钮还可以添加到具有特定内容类型的列表或库中。这允许您基于内容类型来确定功能区自定义项的目标位置。

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <ContentType ID="0x01AB" Name="My Custom Content Type" Group="My Custom Content Types" Description="A customized content type." Version="1">
    <FieldRefs>
      <FieldRef ID="{8c06beca-0777-48f7-91c7-6da68bc07b69}" Name="Created" DisplayName="Created By"/>
      <FieldRef ID="{1df5e554-ec7e-46a6-901d-d85a3881cb18}" Name="Author" DisplayName="Author Name"/>
    </FieldRefs>
  </ContentType>
  <CustomAction Id="ContentTypeRibbonCustomization" RegistrationId="0x01AB" RegistrationType="ContentType" Rights="ManagePermissions" Location="CommandUI.Ribbon.ListView" Sequence="95" Title="Ribbon Customization for a Content Type">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
          <Button Id="ContentTypeTest.Button" Command="ContentTypeCommand" CommandType="General" Description="Redirects to Settings.aspx" TemplateAlias="o2" Sequence="95" LabelText="Settings"/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="ContentTypeCommand" CommandAction="/_layouts/Settings.aspx" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>

向"网站操作"菜单添加项目

SharePoint Foundation 中的每个页面上都存在"网站操作"菜单。您可以使用以下自定义操作向此菜单添加其他项目。

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="SiteActionsToolbar" GroupId="SiteActions" Location="Microsoft.SharePoint.StandardMenu" Sequence="1000" Title="Custom Site Settings">
    <UrlAction Url="/_layouts/Settings.aspx"/>
  </CustomAction>
</Elements>

添加编辑控制块菜单项

编辑控制块 (ECB) 是针对每一项的菜单,它用于列表项。

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
  <CustomAction 
    Id=" ECBItemCustomization"
    RegistrationType="List"
    RegistrationId="101"
    Location="EditControlBlock"
    Sequence="106"
    Title="Navigate to Site Settings">
    <UrlAction Url="/_layouts/Settings.aspx"/>
  </CustomAction>
</Elements>

请参阅

概念

默认自定义操作位置和 ID

SharePoint Foundation 中的服务器功能区

Feature.xml 文件