Add List Item Menu custom action to a specific content type (SP 2010)

Requirement:

I need to add a custom action List Item Menu for documents in a document library that have CT1 content type. The List Item Menu must be visible only for Contributors.CustomAction

Solution:

I have to confess, I wanted to do it the easy way using SharePoint Designer but it cannot be done for a specific content type (it is mentioned here, I trust that guy).

There are two options:
1. create a feature in Visual Studio 2010:


 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="MyFeatureName.ListItemMenu" <- unique identifier
   RegistrationType="ContentType"
   RegistrationId="0x0101001DFBE40A5ABC3C49AE50D8FD4539728F" <-unique Id, taken from SP Designer

   Location="EditControlBlock"
   Title="Edit Record"
   Sequence="1" <- first item in the context menu
   Rights="EditListItems"
   ImageUrl="/_layouts/IMAGES/EDITITEM.gif" <-same image as "Edit Properties"
   Description="Update record metadata">
  <UrlAction Url="http://someServer/someWebApp/someController/Index/{ItemId}" /> <-redirecting to another application, based on the current item Id

 </CustomAction>
 </Elements>

2. create a PowerShell script:


 Add-PSSnapIn "Microsoft.SharePoint.PowerShell" -EA 0 <- add SP library (optional)
 $web = Get-SPWeb http://myserver/test/app
 $action = $web.UserCustomActions.Add();
 $action.RegistrationType = "ContentType";
 $action.RegistrationId = "0x0101001"; <- CT1 identifier, taken from SP Designer

 $action.Location = "EditControlBlock";
 $action.Title = "Edit Record";
 $action.Sequence = 1; <- first option in the menu
 $action.Rights = "EditListItems"; <- rights only for contributors
 $action.Url = "http://someServer/someWebApp/someController/Index/{ItemId}"; <- redirecting to another page based on the document Id

 $action.Update();
 $web.Dispose()

In case you wonder how to map SPBasePermissions to Viewers/Contributors roles for "Rights Mask" check here or here

Note: it has been tested carefully.

In case you are wondering if you can add this custom action to a content type from a content type hub, the answer is yes it works, we used the powershell script. Take care that RegistrationId unique identifier is uppercase! Sometimes "RegistrationType" must be cast to the type:
$action.RegistrationType = [Microsoft.SharePoint.SPUserCustomActionRegistrationType]::ContentType

About these ads


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

Follow

Get every new post delivered to your Inbox.