Create a Custom Action for a specific list via Visual Studio 2013 in SharePoint 2013

In this article, I will demonstrate How to Create a Custom Action for only one List via Visual Studio 2013 in SharePoint 2013.

Scenario,

I need to add a new Tab to Display Form Ribbon for only one list in SharePoint 2013 to be like this :

Custom Action Tab at display form

Steps :

  • Open Visual Studio > Create a new empty SharePoint solution .

Empty SharePoint Solution

  • Select Deploy as a farm solution .

Deploy as farm solution

  • Right Click on Project Name. > select Add.

Add item in Visual studio

  • Add New Item.

Add new item in Visual studio

  • Add Module item.

Create Module in visual studio

  • Delete Sample.txt file.

Delete Sample file

  • Remove the current code at  Element.xml.

Module structure in visual studio

  • Paste the following code at Element.xml.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="CustomRibbonTab"
Location="CommandUI.Ribbon.DisplayForm"
RegistrationId="0x0100E076A4E49D4B5D47BDF993EC507AE002"
RegistrationType="ContentType">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.Tabs._children">
<Tab
Id="Ribbon.CustomTab"
Title="Custom Tab"
Description="Custom Tab !!!"
Sequence="501">
<Scaling
Id="Ribbon.CustomTab.Scaling">
<MaxSize
Id="Ribbon.CustomTab.MaxSize"
GroupId="Ribbon.CustomTab.CustomGroup"
Size="OneLargeTwoMedium"/>
<Scale
Id="Ribbon.CustomTab.Scaling.CustomTabScaling"
GroupId="Ribbon.CustomTab.CustomGroup"
Size="OneLargeTwoMedium" />
</Scaling>
<Groups Id="Ribbon.CustomTab.Groups">
<Group
Id="Ribbon.CustomTab.CustomGroup"
Description="Custom Group!"
Title="Custom Group"
Sequence="52"
Template="Ribbon.Templates.CustomTemplate">
<Controls Id="Ribbon.CustomTab.CustomGroup.Controls">
<Button
Id="Ribbon.CustomTab.CustomGroup.CustomButton"
Command="CustomTab.CustomButtonCommand"
Sequence="15"
Description=""
LabelText="People"
Image32by32="/_layouts/images/PPEOPLE.GIF"
TemplateAlias="cust1"/>
</Controls>
</Group>
</Groups>
</Tab>
</CommandUIDefinition>
<CommandUIDefinition Location="Ribbon.Templates._children">
<GroupTemplate Id="Ribbon.Templates.CustomTemplate">
<Layout
Title="OneLargeTwoMedium"
LayoutTitle="OneLargeTwoMedium">
<Section Alignment="Top" Type="OneRow">
<Row>
<ControlRef DisplayMode="Large" TemplateAlias="cust1" />
</Row>
</Section>
</Layout>
</GroupTemplate>
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="CustomTab.CustomButtonCommand"
CommandAction="javascript:alert('Hello, world!');" />
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>

The important section of the above code is

If you need to apply the custom action for all custom lists in DisplayForm so it should look like the following.

RegistrationId Custom Action list

In my case, I only need to create a custom action for only one list in DisplayForm so

  • Location = “CommandUI.Ribbon.DisplayForm
  • RegistrationId = “Content Type ID
  • RegistrationType  = “ContentType

So I should first create a site content type for this list by following the mentioned steps below:

  • Go to List Settings. (Make sure that the content types has been allowed).
    • Advanced Settings > Allow management of content types.

allow management of content types

  • Go to Site Settings > Web Designer Galleries > Site Content Type.

Web Designer Galleries - Site Content Types

  • Click Create.

Create Site content types 1

  • Set the name of the content type and set the other entries as the following:

New Site Content Type

  • Go to List settings > below content types > click on Add from existing site content types

Add from existing site content types

  • Add the content type that you created

Add Content Type

  • Set it as default by clicking on Change new button order and default content typeCreate Site Content Types
  • Check it and uncheck the default item content type > and select its order to 1.

Change Content Type order

  • It’s should look like

Content types list

  • Now you should get Content Type ID by following the mentioned steps at Getting Content Type ID of SharePoint List.
  • The final settings for location and registration id and registration type should look like the following

Custom Action list for a Content Type

  • Deploy the solution > go to your list > add new Item > display it > it should look like the following :

Custom Action Tab at display form

Enjoy 🙂

Please don’t hesitate to ask for more clarifications or request the source code at comments.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s