SharePoint Custom Actions in a list view WebPart

Search

Accessible SharePoint WebSites
Download ARF

SharePoint Custom Actions in a list view WebPart

http://blog.thekid.me.uk

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.

Posted by Vincent Rothwell on Tuesday, 29 Apr 2008 18:06  - 23 Comments
Orininally printed from http://thekid.me.uk - Copyright Vincent Rothwell 2007
 

Comments

Sunday, 27 Jul 2008 10:36 by Paul Liebrand
Have you figured out why this method does not work when the GroupId is set to "NewMenu"?

Sunday, 27 Jul 2008 10:36 by Vince
Paul, Must admit I have never tried it and so didn't know it was a problem. I'll have a look when I get a chance. --Vince

Sunday, 27 Jul 2008 10:36 by rajesh
I'm trying to achieve the same for teh DisplayFormToolBar. I believe I have to inherit from ToolBarMenuButton class instead of webcontrol. How do I set the custom action in this control and add this control to the displayformtoolbar?

Sunday, 27 Jul 2008 10:36 by rajesh
I'm trying to achieve the same for teh DisplayFormToolBar. I believe I have to inherit from ToolBarMenuButton class instead of webcontrol. How do I set the custom action in this control and add this control to the displayformtoolbar?

Tuesday, 29 Jul 2008 08:28 by Darlan Batista
Paul, if you wanna render the CustomAction inside NewMenu Group you must inherit MenuItemTemplate instead of WebControl. eg.: public class ActionMenu : MenuItemTemplate { protected override void OnLoad(EventArgs e) { EnsureChildControls(); base.OnLoad(e); } protected override void CreateChildControls() { this.Text = "Test..."; this.ClientOnClickNavigateUrl = "~site/Aes/InserirAtividade.aspx"; base.CreateChildControls(); } }

Thursday, 31 Jul 2008 01:46 by Turkiye
Thanx You.. Perfect Docs I'm trying to achieve the same for teh DisplayFormToolBar. I believe I have to inherit from ToolBarMenuButton class instead of webcontrol. How do I set the custom action in this control and add this control to the displayformtoolbar?

Tuesday, 30 Sep 2008 06:21 by wnhacjod olrgvb
hfjmiy nxspybmf amdgox zjdueh rqxbanf uplh jeom

Thursday, 13 Nov 2008 12:55 by TJ
I've been trying to add a CustomAction to the EditControlBlock location (item level context menu). However I want whether the link renders (is visible) dependent on some logic. I've been trying to get an assembly working with a CustomAction in the EditControlBlock but no such luck. To be honest I'm trying to find an example where the item level context menu is extended BUT is dependent on logic to work out whether it is visible/disabled. It seems strange that it must be globally available and logic can't disable it, Microsoft obviously manage it with "check in", and "discard checkout" etc. any ideas how?

Friday, 21 Nov 2008 03:02 by Vince
TJ, You can't really have logic applied as the items are rendered once on the page (not per item) and then displayed (via JavaScript) when needed. There are possibilities around ContentTypes which would give you some control, but not much. If you really need to perform custom logic you will need to do it via JavaScript... // Add menu item CAMOpt(m, "Print Item...", "_tk_printListItem()", "/_layouts/images/fax.gif"); // Add separator CAMSep(m); Allows you to add menu items in JS. You will need to override a javascript function, which is there for this very purpose... // Attach to the item menus // Attach to the item menus if (typeof(Custom_AddListMenuItems) == "function") _tkp_previous_Custom_AddListMenuItems = Custom_AddListMenuItems; Custom_AddListMenuItems = _tk_printItem_Custom_AddListMenuItems; You can download a working example here... http://blog.thekid.me.uk/archive/2007/07/22/easily-print-sharepoint-lists-using-your-own-template.aspx HTH, Vince

Friday, 2 Jan 2009 04:23 by

Wednesday, 28 Jan 2009 02:09 by Brent
Extremely helpful. Thank you!

Wednesday, 28 Jan 2009 03:26 by Brent
One other thought. Make sure to register the control you create in the SafeControls entry of your config file. This probably seems obvious, but as a SharePoint noob I had to wrestle with this for about an hour before a coworker pointed the problem out. Thanks, Vincent!

Tuesday, 24 Feb 2009 09:53 by Adam
If you want to add a custom action to the DisplayFormToolbar you should inherit from Microsoft.SharePoint.WebControls.FormButton. Use Reflector to look at the NewItemButton, EditItemButton or DeleteItemButton for some further details.

Monday, 9 Mar 2009 06:28 by peter
Now I have requirement which need to add the custom content menu item to picture library. Could someone tell me how?

Tuesday, 7 Jul 2009 08:32 by Anu
I tried this sample code but iam unable to get the menu item in ActionsMenu. If i remove the ControlAssembly and ContorlClass ActionsMenu item is available. What could be the gone wrong? My requirement is to display a menu item depending on the logged in user permissions. I do have a group, only this group users should see the added menu item. can this be done. Please help me in this case. Am struggling since2 days. Finally reached this post. though my struggle ends here but no. no solution? Please help me in this. Thanks in advance.

Saturday, 18 Jul 2009 07:24 by
Hi!amlx! http://pyfyyomr.com ezqto tviqu

Monday, 20 Jul 2009 03:09 by Sbst
I got it working with the example, but the thing what keeps me busy for days now is why it works on all the default lists, but when i create a list like : SPList oList = web.Lists[new Guid(value[1])]; lvList = new ListView(); ViewToolBar toolbar; toolbar = new ViewToolBar(); SPContext context = SPContext.GetContext(this.Context, oList.DefaultView.ID, oList.ID, web); toolbar.RenderContext = context; content.Controls.Add(toolbar); lvList.ListId = oList.ID.ToString("B").ToUpper(); lvList.ViewId = oList.DefaultView.ID.ToString("B").ToUpper(); content.Controls.Add(lvList); The list is the same,i see my items and the folder. The 'delete all' menuitem is available. But not the up folder menuitem. When i look at the same list like /lists/aaa/allitems.aspx it works. So its not a dll / namespace problem i think. Do you have any idea what the differences could be?

Monday, 20 Jul 2009 03:38 by Sbst
Never mind! As always when you try to descript the problem as clear as possible, you find out what the problem is. When creating a listview the listview class is used. This example you described is based on the listviewebpart. A simple type problem which i didn't find out because it was in a try/catch. I'll try to rewrite the code based on the RootFolder querystring var for my custom list.

Tuesday, 4 Aug 2009 06:22 by Thiyagarajan
Iam Trying the same Logig but it is not working for Me, Any prompt help would be highly appriciated.

Tuesday, 12 Jan 2010 08:51 by Japie
Also getting the following: I tried this sample code but iam unable to get the menu item in ActionsMenu. If i remove the ControlAssembly and ContorlClass ActionsMenu item is available. What could be the gone wrong?

Tuesday, 9 Feb 2010 04:42 by Jeff
I have the same issue as Anu and Japie....the menu item is not showing up in the ActionsMenu, but if I remove the ControlAssembly and ControlClass from the CustomAction element, the menu item shows up. I have already added the SafeControl to the web.config. I would be grateful for any suggestions!

Friday, 19 Feb 2010 03:08 by Bharat Sukhwal
What will contain in thekid.aspx in "/_layouts/thekid.aspx?ListId=" + oListView.ListName;? Means, what will the code for that page?

Thursday, 8 Apr 2010 11:54 by rfarqleet
I completely agree with all that here is told Your blog about SharePoint Custom Actions in a list view WebPart helped us a lot. thank you!



Url

Email

Comments