Friday, November 20, 2009

How To: Make an ECB menu item for a folder

Technorati Tags:

There seems to be a lot of information on how to create a “Edit Control Block” for a list item in SharePoint. An ECB is a menu item that shows up when you click on an item in a SharePoint list or document library. You can generally make these by defining a feature and a custom action xml file. In the custom action xml file you set certain attributes of the “CustomAction” element. The most important of which is the UrlAction attribute which can be a url which the user will be redirected to when clicking on the item.

http://msdn.microsoft.com/en-us/library/ms460194.aspx

The location attribute determines where the menu item will be displayed. Setting the location to “Edit Control Block” tells SharePoint to display the menu item in the context menu for the list item. In a document library you can further refine when the menu item is displayed by setting the RegistrationType to “FileType” and the RegistrationId to a file extension like “tif”. Then the menu item will only show up for tif files.

<CustomAction RegistrationType="FileType" RegistrationId="tif" Location="EditControlBlock" Sequence="106" Title="View" ImageUrl="/_layouts/images/view.gif">
  <UrlAction Url="_layouts/settings.aspx"/>
</CustomAction>


So how do I make this menu item just for folders in a list or library. Well you use the RegistrationType set to “ContentType” and the RegistrationId set to “0x0120” which is the contenttype id for a folder in SharePoint.



<CustomAction RegistrationType="ContentType" RegistrationId="0x0120" Location="EditControlBlock" Sequence="106" Title="View" ImageUrl="/_layouts/images/view.gif">
  <UrlAction Url="_layouts/settings.aspx"/>

</CustomAction>



Finally, you can actually call javascript from your UrlAction Url attribute. The following shows how to construct a querystring detecting whether to use https or http for the querystring.


<CustomAction RegistrationType="FileType" RegistrationId="tif" Location="EditControlBlock" Sequence="106" Title="View" ImageUrl="/_layouts/images/view.gif">
  <UrlAction Url="javascript: var isHttps = window.location.href.indexOf('https') == 0; var i = (isHttps) ? 8 : 7; var itemUrlToken = '{ItemUrl}'; var siteUrlwoHttp = window.location.href.substring(i) ; var index = siteUrlwoHttp.indexOf('/'); var itemUrl = ((isHttps) ? 'https://' : 'http://') + siteUrlwoHttp.substring(0, index) + itemUrlToken; window.open('/_layouts/custompage.aspx?doc=' + itemUrl);" "/>
</CustomAction>


 

1 comment:

Anonymous said...

Hi,
I wanted to display custom Edit control block into the each listitem not in a folder, Please guide me to resolve this issue.

Thanks
rishkarthik

Post a Comment