Custom Actions are useful as they allow you to add functionality to a list without having to write any code. Using the UrlAction node of the CustmAction node you can create a link to a page passing the ListID using List={ListId} in the URL. Normally this works very well, however when the list is displayed in a list view WebPart the {ListId} parameter is not replaced!!
Fortunately there is a way around this problem, but you will have to write some code.
Along with the UrlAction node, there are other attributes you can add which will allow you to generate the link using C#. The following shows how you can define a CustomAction which uses code to generate the link, without using the UrlAction parameter.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="TheKid.Samples.ActionMenu"
Location="Microsoft.SharePoint.StandardMenu"
GroupId="ActionsMenu"
Title="My Action"
ControlAssembly="TheKid.Samples, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4e437110aa8c4c12"
ControlClass="TheKid.Samples.ActionMenu" />
</Elements>
Here you can see that the UrlAction has been replaced with an assembly and a class...these will create our link for us. The ActionMenu class is just a standard System.Web.UI.WebControl which overrides CreateChildControls....
public class ActionMenu : WebControl
{
protected override void OnLoad(EventArgs e)
{
EnsureChildControls();
base.OnLoad(e);
}
protected override void CreateChildControls()
{
ListViewWebPart oListView = FindListView(Parent);
if (oListView == null) return;
MenuItemTemplate menu = new MenuItemTemplate();
menu.Text = "Test...";
menu.ClientOnClickNavigateUrl = "/_layouts/thekid.aspx?ListId=" + oListView.ListName;
Controls.Add(menu);
base.CreateChildControls();
}
protected ListViewWebPart FindListView(Control oParent)
{
if (oParent is ListViewWebPart) return oParent;
if (oParent.Parent == null) return null;
return FindListView(oParent.Parent);
}
}
Here we see the control creating a new MenuItemTemplate and adding it to the controls collection. It also looks for the Parent ListViewWebPart in order to get the list ID. This should work no matter where the CustomAction is being used.
I also have another custom action sample providing an 'Up Folder' button to a ListViewWebpart.