7 Jun 2012

Listing All Custom Actions in SharePoint 2010

Listing all the custom actions supported in SharePoint 2010.
To help you explore all the customization possibilities for the SharePoint 2010 UI, you can create a simple HTML table containing all the custom actions available to enabled features in SharePoint 2010. This table contains values for the properties relevant to each action, including
  • Feature
  • Id
  • GroupId
  • Location
  • Sequence
  • RegistrationType, and
  • RegistrationId
You can start by placing a repeater control into the page to display the properties and then adding each property to the table in turn, as shown in this code:
<%@ Page Language="C#" AutoEventWireup="true"
DynamicMasterPageFile="˜masterurl/default.master"
CodeFile="ListAllCustomActions.aspx.cs" Inherits="ListAllCustomActions"
CodeFileBaseClass="Microsoft.SharePoint.WebControls.LayoutsPageBase" %>
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderMain" runat="server">
<asp:Repeater runat="server" ID="rptCustomActions" EnableViewState="false">
<HeaderTemplate>
<table>
<tr>
<td class="ms-vh2">Feature</td>
<td class="ms-vh2">Id</td>
<td class="ms-vh2">Location</td>
<td class="ms-vh2">GroupId</td>
<td class="ms-vh2">Sequence</td>
<td class="ms-vh2">RegistrationType</td>
<td class="ms-vh2">RegistrationId</td>
</tr>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="ms-vb2"><%# Eval("Feature") %></td>
<td class="ms-vb2"><%# Eval("Id") %></td>
<td class="ms-vb2"><%# Eval("Location") %></td>
<td class="ms-vb2"><%# Eval("GroupId") %></td>
<td class="ms-vb2"><%# Eval("Sequence")%></td>
<td class="ms-vb2"><%# Eval("RegistrationType")%></td>
<td class="ms-vb2"><%# Eval("RegistrationId")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</asp:Content>

The code-behind for this application page contains a Page_Load method. This method contains code that iterates through all the available features on the current SharePoint farm. The parameters are defined by SPFarm.Local.FeatureDefinitions. The custom action element definitions for each feature are found, and their attributes are collected into a helper class called CustomActionContainer. Finally, a list of CustomActionContainer objects is bound to the repeater and returned.
public partial class ListAllCustomActions : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
List<CustomActionContainer> containers = new List<CustomActionContainer>();
foreach (SPFeatureDefinition feature in SPFarm.Local.FeatureDefinitions)
{
containers.AddRange(FindCustomActionsForFeature(feature));
}
rptCustomActions.DataSource = containers;
rptCustomActions.DataBind();
}
protected List<CustomActionContainer>
FindCustomActionsForFeature(SPFeatureDefinition feature)
{
List<CustomActionContainer> retVal = new List<CustomActionContainer>();
foreach (SPElementDefinition element in
feature.GetElementDefinitions(CultureInfo.CurrentCulture))
{
if (element.XmlDefinition.Name == "CustomAction")
{
CustomActionContainer c = new CustomActionContainer();
c.Feature = feature.DisplayName;
c.Id = GetAttributeValue(element.XmlDefinition,"Id");
c.GroupId = GetAttributeValue(element.XmlDefinition, "GroupId");
c.Location = GetAttributeValue(element.XmlDefinition, "Location");
c.Sequence = GetAttributeValue(element.XmlDefinition, "Sequence");
c.RegistrationType = GetAttributeValue(element.XmlDefinition,
"RegistrationType");
c.RegistrationId = GetAttributeValue(element.XmlDefinition,
"RegistrationId");
retVal.Add(c);
}
}
return retVal;
}
private String GetAttributeValue(XmlNode node, String attributeName)
{
String retVal = String.Empty;
if (node.Attributes[attributeName] != null)
retVal = node.Attributes[attributeName].Value;
return retVal;
}
}
public class CustomActionContainer
{
public String Feature { get; set; }
public String Id { get; set; }
public String GroupId { get; set; }
public String Location { get; set; }
public String Sequence { get; set; }
public String RegistrationType { get; set; }
public String RegistrationId { get; set; }

No comments: