By Joe Ferner on May 10, 2009 12:35 PM
If you have ever tried adding a SharePoint custom action to the actions menu and tried using "List" or "ContentType" as the "RegistrationType" and then tried to specify a specific list or content type in the "RegistrationId" you know that it doesn't work. SharePoint will silently not render your custom action. If you try and target a generic list using a "RegistrationId" of "100" you will see that SharePoint will gladly render your action on every list in the site. I have found a rather kludgy work around to the problem.
First off in your element manifest file you need to use the "ControlAssembly" and "ControlClass" attributes off of CustomAction. MSDN doesn't say a whole lot about these attributes but essentially they allow you to specify a web control class in your assembly which can render the action. (If you need to create hierarchical menus check out this article.) Here is the XML to get you started:
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <CustomAction Id="MyCustomAction" RegistrationType="List" GroupId="ActionsMenu" Location="Microsoft.SharePoint.StandardMenu" Sequence="1000" ControlAssembly="[Fully qualified assembly name]" ControlClass="MyNamespace.MyCustomAction"> </CustomAction> </Elements>
Next you will need to create a web control class which renders the menu item, as seen below:
public class MyCustomAction : WebControl { private MenuItemTemplate _action; protected override void CreateChildControls() { SPWeb site = SPContext.Current.Web; _action = new MenuItemTemplate { Text = "My Action", Description = "My Action", ImageUrl = "/_layouts/images/NEWITEM.GIF", ClientOnClickNavigateUrl = "http://www.nearinfinity.com" }; Controls.Add(_action); } }
Compile and deploy your solution. Oh and make sure to add your assembly and namespace to the safe control list or you will end up pulling your hair out because once again SharePoint will silently not render your menu item. You should now see the "My Action" on every list in the site. We have now reproduced what a standard CustomAction element in your element manifest would do normally. Now we need to find a way to determine which list our control is being added to. The best way I found for doing that is to traverse the control hierarchy up the parent chain until I found the containing ListViewWebPart. The code is quite simple and looks like this:
private ListViewWebPart GetParentListViewWebPart() { Control parent = Parent; while (parent != null) { if (parent is ListViewWebPart) { return (ListViewWebPart)parent; } parent = parent.Parent; } return null; }
You should know where I'm going with this now, but for completness I will show you the last piece of code below which restricts it to a single list instance:
private Guid TARGET_LIST_ID = new Guid("0D9B9302-8599-4CE5-8695-1B95FE7378F1"); protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (!Page.IsPostBack) { EnsureChildControls(); _action.Visible = false; ListViewWebPart listView = GetParentListViewWebPart(); if (listView != null) { Guid listGuid = new Guid(listView.ListName); if (TARGET_LIST_ID == listGuid) { _action.Visible = true; } } } }
Now that we have the list Guid that our control is being rendered for I will leave it as an exercise to the reader to restrict it to a given content type.
Why SharePoint doesn't provide this kind of functionality out of the box amazes me, but at least they provide a powerful API that you can get in there and work around it in a not so horribly kludgy way. Maybe SharePoint 2010 will include this :)
发表评论
-
Creating Hierarchical Menus with a CustomAction in SharePoint
2011-11-17 14:11 884原文链接 http://weblogs.asp.net/j ... -
SharePoint Custom Action Identifiers
2011-11-16 13:48 921原文链接 http://johnholliday.net/ ... -
Filtering with SPGridView
2011-10-12 15:23 751原文链接 http://vspug.com/bobsbon ... -
SPGridView: Adding paging to SharePoint when using custom data sources
2011-10-11 16:55 1046原文链接 http://blogs.msdn.com/b/ ... -
SPGridView and SPMenuField: Displaying custom data through SharePoint lists
2011-10-11 16:14 1268原文链接 http://blogs.msdn.com/b/ ... -
How to work around bugs in the SPGridView control
2011-10-11 16:10 793原文链接 http://blogs.msdn.com/b/ ... -
SharePoint – Adding ECB Menu Item for Specific Custom List
2011-10-11 10:52 901原文链接 http://www.csharpest.net ... -
Adding CheckBoxes in SharePoint GridView (SPGridView)
2011-10-10 17:01 920原文链接 http://www.c-sharpcorner ... -
SharePoint的列级安全性
2011-10-10 11:25 1131原文链接 http://www.infoq.com/cn/ ... -
在 SharePoint 中自定义审核
2011-10-09 16:59 1696原文链接 http://msdn.micr ... -
SharePoint 2007 and Windows WorkFlow Foundation: Integrating Divergent Worlds
2011-10-09 16:32 1382原文链接 http://www.developer.com ... -
通过 STSDEV 简化 SharePoint 开发
2011-09-30 15:58 1241原文链接 http://msdn.microsoft.co ... -
计算字段公式
2011-09-30 15:47 851原文链接 http://msdn.microsoft ...
相关推荐
ASP.NET Core 1.1 For Beginners: How to Build a MVC Website by Jonas Fagerberg English | 19 May 2017 | ASIN: B071VX7KN4 | 411 Pages | PDF | 6.66 MB Want to learn how to build ASP.NET Core 1.1 MVC Web ...
Simple Actions Menu for KDE
为SharePoint 2010 Workflow 开发 Custom Workflow Activity(Develop Custom Workflow Activity for SharePoint 2010 Workflow). SharePoint2010提供了很多有用的开箱即用的Activity(活动action),我们可以在...
Add a language definition to your wordfile for use with UltraEdit and UEStudio's powerful syntax highlighting Syntax highlighting and code folding Explanation of highlighting and folding definitions ...
A curated list of awesome actions to use on GitHub
The example also shows how to store information separately in both a list and a dictionary in order to create the header row and the data rows for the output file. This is a reminder that you can ...
The Maya guide for animators, How to Cheat in Maya 2012 presents everything you need to know about character animation in Maya. Fully updated for the latest revision of Maya, this book provides you ...
Module 8: Introduction to Data Analysis [removed]DAX)This module describes how to use DAX to create measures and calculated columns in a tabular data model.Lessons DAX Fundamentals Using DAX to ...
To add or edit a new shotcut, go to /res/xml/shortcuts.xml : Handle Actions To handle shortcuts, just add new constant: private final static String CUSTOM_ACTION = "custom_action"; and check the ...
I will show how to do this at a later date.CopyHook Handler - Hook in to Folder and Printer Moves, Copies, Deletes and Renames and either allow, deny any single action in multiple actions, or cancel ...
在你编写as3代码的时候会用到很多类对吧,那些是系统自带的,有这个工具你可以编写自己的类,然跟使用系统的一样。
items a blank line is added to the list (EnumProp.Add('')). - ADD: Added the method TEnumProp.SetEnumType, setting all enumerable type items in compliance with the Delphi type: SetEnumType...
you'll explore various robust sculpting methods supported by Blender that allow you to edit objects with actions such as bends or curves, similar to drawing or building up a clay structure of ...
You will learn to build your own modules to perform actions specific to your business. By the end you will create an entire cluster of virtualized machines, all of which have your applications and ...
The LLDB command command regex acts much like command alias, except you can provide a regular expression for input which will be parsed and applied to the action part of the command. 10. Assembly ...
profiles, and how to build custom Route classes to obtain even more flexibility. Chapter 7, Creating and Consuming Web Services: Web services are essential when looking forward to expose application ...
This project model is not meant to be a tool to justify creating impositions for developers, but as a tool to facilitate coordination. It is meant as a description of the project, with an overview of...
specify events and event handling to produce animation and respond to user actions. You can include video and audio on your web site with standard controls, or place the video or audio in your ...
You’ll discover how to deploy workflow template files and features, program SharePoint external data exchange, send WCF messages from a SharePoint workflow, and develop custom Office SharePoint ...
These actions are all fairly simple to implement because this app is built on a SQLite database. I made a 4-minute video describing the bascis of how the SQLite Database works in this app: ...