Dailycode.info

Short solution for short problems

Custom sharepoint list item action

I thought this was a often used functionality of WSS, but when I started googling I guess I was wrong.

I needed to add some custom action to the list items in my document list. Sharepoint made this very easy to do. This article describes it pretty good: http://msdn.microsoft.com/en-us/library/ms473643.aspx.

I followed the example, only using this custom action: UserInterfaceCustomActions.ECBItemToolbar. This is the one that is shown in the action toolbar of an item. Here is the result:

Suggest modification on a document                                       Copy a document to a public library or resend an instruction on a document.

       

 As I was following this example I noticed that when I tried to install the feature there was an error in the XML:

Feature definition with Id 2aeaccad-cf77-4d38-86a2-4fca58bbc0cc failed validation, file 'UICustomActions.xml', line 8, character 5:
The 'Type' attribute is not declared.

This is how it looked like at the Microsoft site:

<!-- Per Item Dropdown (ECB)-->  
<CustomAction
      Id="UserInterfaceCustomActions.ECBItemToolbar"
     RegistrationType="List"
     RegistrationId="101"
     Type="ECBItem"
      Location="EditControlBlock"
     Sequence="106"
     Title="Copy to public">
     <UrlAction Url="/_layouts/CustomActions.aspx?ECBItem"/>  

</CustomAction>


This is how it should look like:

<!-- Per Item Dropdown (ECB)-->
 <CustomAction Id="UserInterfaceCustomActions.ECBItemToolbar"
 RegistrationType="List"
 RegistrationId="101"
 Location="EditControlBlock"
 Sequence="106"
 Title="MY ECB ITEM">
  <UrlAction Url="/_layouts/CustomActionsHello.aspx?ECBItem"/>

</CustomAction>


The Type attribute is not allowed. Get rid of the "Type" attribute and it will work, strange that Microsoft has this in their example. When you want to deploy the item to all type of list on the website, remove the RegistrationId attribute. For a custom list type the registrationId = "100".  RegistrationId="101" is for document libraries.

You can find a list of all attributes for the custom action element here: http://msdn.microsoft.com/en-us/library/ms460194.aspx.

If you like to add an image to your action, use the ImageUrl attribute:   

Rights="ViewListItems"
ImageUrl="/_layouts/images/CHNGCOL.GIF"
Title="Suggest modification">

      

After I installed and activated the feature, I needed to restart IIS and it worked. Then I made some changes to the XML and wanted to update the feature. Here is the command to do that:

stsadm -o installfeature -filename UserInterfaceCustomActions\feature.xml -force 

 

Followed by an iisreset everytime you made changes.

So it's very easy to do this. You can use a webpart page in combination with custom webparts to handle the action. When you create new features, do not forget to generate a unique guid for each new feature. Its easy to copy and paste an existing feature and then change the custom actions, but if you forget to change the feature id, then you will get a warning message when you try to install or activate the feature. It could be something like: "feature is already installed" or "feature is already activated". When you use the -force attribute, you will overwrite the old feature with the same id. So be cautious that you provide each feature with an unique Id!

When you would like to install different features for different sites, create as many directories in the Features dirrectory as you like. I used UserInterfaceCustomActions as directory like the example, but this is not a mandatory name, when you start developing, choose the names of these directories wisely, so you can distinguish the features. E.g. MyCompanyTaskManagement\Feature.xml and MyCompanyFinance\Features.xml. This way we instantly know what the features are for. (As described in the paragraph above, provide each feature with an unique feature id)

As I was deploying the first features to test sites, I noticed that I only could deploy to the entire site collection. If I tried to deploy to a subsite I got this error: The specified feature applies to the entire site collection, but the specified URL refers to a particular sub site.  To apply this feature to the entire site collection, use the root URL ...  At first I couldn't find the problem, because I was trusting on the example on the Microsoft site. But for the second time I noticed there was an error in their example. In the feature.xml they put "Site" as scope:

<?xml version="1.0" encoding="utf-8"?>
 <Feature Id="6FA98D7A-D802-4f30-8473-0F9B00DD8F7C"
    Title="UI Custom Actions"
    Description="Custom action on list items."
    Version="1.0.0.0"
    Scope="Site"
    xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="CustomActions.xml" />
  </ElementManifests>

</Feature>

 

Then they suggest this for the activation of the feature:

stsadm -o activatefeature -filename UserInterfaceCustomActions\feature.xml -url http://Server/Site/Subsite

 

Which is not correct, because if they use Site as scope, it is not permitted to activate it to a subsite, but it is required to activate it on site collection level. So the solution to this problem is: Use scope="Web" in the feature.xml. Then it is allowed to activate it for a subsite!

You can find the scopes and more on the feature.xml file here.

You can find more on the activatefeature here. You can deactivate a feature using the deactivatefeature:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN>stsadm -o deactivatefeature -filename UserInterfaceCustomActions\feature.xml -url http://Server/Site/Subsite

When you need security on your feature, its really easy. Just add the Rights to the custom action XML. The list of all the rights you can use can be found here.I used the AddListItems to make sure only users with sufficient rights could view the feature. Here's the example of the XML:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="UserInterfaceCustomActions.ECBItemToolbar"
    RegistrationType="List"
    RegistrationId="100"
    ImageUrl = "/_layouts/images/WRI16.GIF"
    Location="EditControlBlock"
    Sequence="106"
    Title="Add progress">
    <UrlAction Url="serverurl/TM/default.aspx?qID={ItemId}"/>
  </CustomAction>
  <CustomAction
    Id="UserInterfaceCustomActions.ECBItemToolbar"
    RegistrationType="List"
    RegistrationId="100"
    ImageUrl = "/_layouts/images/EML16.GIF"
    Location="EditControlBlock"
    Sequence="106"
    Rights="AddListItems"
    Title="Send alert">
    <UrlAction Url="serverurl/TM/default.aspx?qMailID={ItemId}"/>
  </CustomAction>
</Elements>
 

As I was developing further, I had 2 different lists on the subsite. Now they both had this functionality available. But I needed to provide the listID as parameter, so I would know which list the items comes from. This I managed by adding an extra querystring to the URLAction. Now it looks like this:

    <UrlAction Url="serverurl/TM/default.aspx?qMailID={ItemId}&amp;qlistID={ListId}"/>

Be sure to check the casing in the code, the first time I wrote {ListID} with capital D, it wouldn't work. Again thanks to Microsoft example. They really have to check their code before posting it public. the & sign has to be replaced by &amp;  

To know more about the custom list item action properties, you can continu to read this blog, or go to the Microsoft site: http://msdn.microsoft.com/en-us/library/ms465980.aspx

Here's a quick list of all Rights you can use for a feature:

AddAndCustomizePages
AddDelPrivateWebParts
AddListItems
ApplyStyleSheets
ApplyThemeAndBorder
ApproveItems
BrowseDirectories
BrowseUserInfo
CancelCheckout
CreateAlerts
CreateGroups
CreateSSCSite
DeleteListItems
DeleteVersions
EditListItems
EditMyUserInfo
EmptyMask
EnumeratePermissions
FullMask
ManageAlerts
ManageLists
ManagePermissions
ManagePersonalViews
ManageSubwebs
ManageWeb
Open
OpenItems
UpdatePersonalWebParts
UseClientIntegration
UseRemoteAPIs
ViewFormPages
ViewListItems
ViewPages
ViewUsageData
ViewVersions

If you want to have several right, seperate them by a comma Eg.:

Rights="AddListItems,ViewPages"

 

Here is a quick list of all locations that can be used:

Location

Custom Action Group ID Group Description
DisplayFormToolbar N/A Location corresponds to the display form toolbar of lists.
EditControlBlock N/A Corresponds to the per-item edit control block (ECB) menu.
EditFormToolbar N/A Location corresponds to the edit form toolbar of lists.
Microsoft.SharePoint.Administration.ApplicationCreated Links Application Created page.
Microsoft.SharePoint.Administration.ApplicationManagement ApplicationSecurity Application Security section on Central Administration Application Management page.
Microsoft.SharePoint.Administration.ApplicationManagement ExternalService ·                       External Service Connections section on Central Administration Application Management page.
Microsoft.SharePoint.Administration.ApplicationManagement SiteManagement ·                       SharePoint Site Management section on Central Administration Application Management page.
Microsoft.SharePoint.Administration.ApplicationManagement WebApplicationConfiguration ·                       SharePoint Web Application Management section on Central Administration Application Management page.
Microsoft.SharePoint.Administration.ApplicationManagement WorkflowManagement Workflow Management section on Central Administration Application Management page.
Microsoft.SharePoint.Administration.Operations BackupRestore Backup and Restore section on Central Administration Operations page.
Microsoft.SharePoint.Administration.Operations DataConfiguration Data Configuration section on Central Administration Operations page.
Microsoft.SharePoint.Administration.Operations GlobalConfiguration ·                       Global Configuration section on Central Administration Operations page.
Microsoft.SharePoint.Administration.Operations LoggingAndReporting Logging and Reporting section on Central Administration Operations page.
Microsoft.SharePoint.Administration.Operations Security Security Configuration section on Central Administration Operations page.
Microsoft.SharePoint.Administration.Operations Topology Topology and Services section on Central Administration Operations page.
Microsoft.SharePoint.Administration.Operations Upgrade Central Administration Operations page.
Microsoft.SharePoint.ContentTypeSettings Fields ·                       Columns section on Site Collection Content Type page.
Microsoft.SharePoint.ContentTypeSettings General ·                       Settings section on Site Collection Content Type page.
Microsoft.SharePoint.ContentTypeTemplateSettings Fields ·                       Columns section on List Content Type page.
Microsoft.SharePoint.ContentTypeTemplateSettings General Settings section on List Content Type page.
Microsoft.SharePoint.Create WebPages Web Pages section on Create page.
Microsoft.SharePoint.GroupsPage NewMenu New menu on site collection People and Groups page.
Microsoft.SharePoint.GroupsPage SettingsMenu Settings menu on site collection People and Groups page.
Microsoft.SharePoint.ListEdit Communications Communications section on Customize page for list or document library.
Microsoft.SharePoint.ListEdit GeneralSettings General Settings section on Customize page for list.
Microsoft.SharePoint.ListEdit Permissions Permissions and Management section on Customize page for list or document library.
Microsoft.SharePoint.ListEdit.DocumentLibrary GeneralSettings General Settings section on Customize page for document library.
Microsoft.SharePoint.PeoplePage ActionsMenu Actions menu on site collection People and Groups page.
Microsoft.SharePoint.PeoplePage NewMenu New menu on site collection People and Groups page.
Microsoft.SharePoint.PeoplePage SettingsMenu Settings menu on site collection People and Groups page.
Microsoft.SharePoint.SiteSettings Customization ·                       Look and Feel section on Site Settings page.
Microsoft.SharePoint.SiteSettings Galleries ·                       Galleries section on Site Settings page.
Microsoft.SharePoint.SiteSettings SiteAdministration ·                       Site Administration section on Site Settings page.
Microsoft.SharePoint.SiteSettings SiteCollectionAdmin ·                       Site Collection Administration section on Site Settings page.
Microsoft.SharePoint.SiteSettings UsersAndPermissions ·                       Users and Permissions section on Site Settings page.
Microsoft.SharePoint.StandardMenu ActionsMenu Actions menu in list and document library views.
Microsoft.SharePoint.StandardMenu ActionsMenuForSurvey Site Actions menu for surveys.
Microsoft.SharePoint.StandardMenu NewMenu New menu in list and document library views.
Microsoft.SharePoint.StandardMenu SettingsMenu Settings menu in list and document library views.
Microsoft.SharePoint.StandardMenu SettingsMenuForSurvey Site Settings links for surveys.
Microsoft.SharePoint.StandardMenu SiteActions Site Actions menu.
Microsoft.SharePoint.StandardMenu UploadMenu Upload menu in document library views.
Microsoft.SharePoint.User ActionsMenu Actions menu on Web site Permissions page.
Microsoft.SharePoint.User NewMenu New menu on Web site Permissions page.
Microsoft.SharePoint.User SettingsMenu Settings menu on Web site Permissions page.
Microsoft.SharePoint.Workflows LeftNavBarLinks Left navigational area on pages for managing workflow.
NewFormToolbar N/A Location corresponds to the new form toolbar of lists.
ViewToolbar N/A Location corresponds to the toolbar in list views.

 

A list for the SharePoint actions with their sequences: http://www.johnholliday.net/resources/customactions.html