Friday, March 21, 2008

CustomAction and {ListId} token bug : JavaScript to the rescue

This bug is widely blogged. You can checkout entries here

http://chrissyblanco.blogspot.com/2007/12/bug-with-customaction-in-actions-menu.html
http://stephenkaye.blogspot.com/2008/02/bug-with-customactions.html

if you define a CustomAction as part of a feature and use tokens in the URL e.g. , the custom action will work fine in a Lists and Document Libraries . However, if you add a web part representing the same List to another page the tokens in the url do not get replaced.

Solution

I am a big believer in javascript. So here is the Javascript solution. After inspecting the page, it turns out that SharePoint creates a ctx javascript object for the list. This object contains a property listName, which holds the ListId property


1) If the MenuItem is yours and you have luxary to change the feature XML code, you coul
construct your element something like

<UrlActionUrl="javacript:window.location='~site/_layouts/pageaspx?List'+ctx.listName"/>


2) If you are using third party feature or if you have issue with out of the box menuitems, you can insert the following Javascript, either by adding a "Content Editor WebPart" or by modifying the aspx page

var __menus = document.getElementsByTagName('ie:menuitem');
for (var i = 0; i <>
itm = __menus(i)
var str = itm.getAttribute("onMenuClick");
if (str && ctx && str.match('ListId') != null )
{ str = str.replace("ListId", ctx.listName)
itm.setAttribute('onMenuClick', str);
}

This code will replace the {ListId} token on all menus items where token is not replaced.


Note : This solution will only work if you have single list on a page. For multiple lists you need to get hold of "ctx" array object in those cases

5 comments:

Madhur said...

This is great .. exactly what I was looking for

Paul said...

Do you have any examples where you can get a hold of the "ctx" array object? What is it called?

Anonymous said...
This comment has been removed by a blog administrator.
Mario Nguyen said...

How to add concatenate "+ctx.ListName" to items on ListActionsToolbar? Here is it's code:

< UrlAction Url="~site/_layouts/AdvancedAlert/AdvancedSubNew.aspx?ListId={ListId}" >

Unknown said...

This is a great blog. Thanks for all your hard work and the info you give. I am hoping you post again soon.I wish u keep on.
Thank You
SharePoint Custom Development