Compartilhar via


How to: Modify the User Interface Using Custom Actions

Applies to: SharePoint Foundation 2010

Using Features makes it easy to add actions to menus and the Server ribbon in Microsoft SharePoint Foundation. The following examples show how to add actions to various menus through a Feature. Each example uses the same project created in SharePoint development tools in Microsoft Visual Studio 2010.

Create the SharePoint Project

  1. Start SharePoint development tools in Visual Studio 2010.

  2. On the File menu, point to New, and then click Project.

  3. In Project Types, under Visual Basic or C#, select Empty SharePoint Project.

  4. Type UserInterfaceActions as the project name. Click OK.

  5. In the SharePoint Customization Wizard, select Deploy as a sandboxed solution. Click Finish.

  6. In Solution Explorer, right-click the UserInterfaceActions project, select Add, and then select New Item.

  7. In the Add New Item dialog box, select the Empty Element template. Enter UserInterfaceActions as the Name.

  8. Open the Elements.xml file.

Add Buttons to the Server Ribbon

The primary set of commands used to interact with SharePoint Foundation is found in the ribbon. The ribbon can be customized using a Feature custom action and ribbon XML. The following steps demonstrate how to modify the ribbon using a custom action and ribbon XML. In each example, the Location attribute of the CustomAction element is different to define the form where the customization will appear. The Location attribute on the CommandUIDefinition element defines where inside the ribbon the customization will appear. For more information about the Server ribbon, see Server Ribbon in SharePoint Foundation.

Add a Button to the List Form

<?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>

Add a Button to the Edit Form

<?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>

Add a Button to the Server Ribbon on a Specific Content Type

A ribbon button can also be added to a list or library with a specific content type. This enables you to target your ribbon customizations based on content type.

<?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>

Add an Item to the Site Actions Menu

The Site Actions menu is available on every page inside SharePoint Foundation. You can add another item to this menu using the following custom action.

<?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>

Add an Edit Control Block Menu Item

The Edit Control Block (ECB) is a per-item menu used for list items.

<?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>

See Also

Concepts

Default Custom Action Locations and IDs

Server Ribbon in SharePoint Foundation

Feature.xml Files